我已经尝试了大约一百万个不同的正则表达式,我无法绕过这一个(不可否认,很多正则表达式都不在我的掌握之中).
在我的文本中,我有这样的变量:
{{$one}}
{{$three.four.five}}
{{$six.seven}}
Run Code Online (Sandbox Code Playgroud)
我有一个包含所有替换的数组('one'的索引是'one'等)但有些可能会丢失.
我想从数组中替换它是否存在,如果不是单独保留文本.
我可以使用什么正则表达式在下面的代码段中的$ text中preg_match_all变量,在适当的地方替换$ replace并回显到浏览器?
<?php
$replaces = array('testa.testb.testc' => '1', 'testc.testa' => '2', 'testf' => '3');
$text = '{{$testa.testb.testc}}<br>{{$testc.testa}}<br>{{$testf}}<br>{{$aaaaa}}<br>';
preg_match_all('/\{\{\$(\w+)\}\}/e', $text, $matches);
foreach($matches as $match)
{
$key = str_replace('{{$', '', $match);
$key = str_replace('}}', '', $key);
if(isset($replaces[$key]))
$text = str_replace($match, $replaces[$key], $text);
}
// I want to end up echo'ing:
// 1<br>2<br>3<br>{{$aaaaa}}<br>
echo $text;
?>
Run Code Online (Sandbox Code Playgroud)
http://codepad.viper-7.com/4INvEE
这个:
'/\{\{\$(\w+)\}\}/e'
Run Code Online (Sandbox Code Playgroud)
就像在片段中一样,是我得到的最接近的.
它也必须使用变量名中的do.
在此先感谢您的帮助!