正则的模式修正符“e”的作用
我记得学php的时候,老师说“e”是配合preg_replace($reg, $replace, $text)。使替换的内容($replace),可以使用正则$reg中的子模式,但以下这个代码却报错:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
$reg = "/(\d{2})\/(\d{2})\/(\d{4})/e";
$text = '01/25/2009到02/02/2009';
$replace = "\${3},\${1}";
echo preg_replace($reg, $replace, $text); //报错syntax error, unexpected ','
……
$replace = "'\${3},\${1}'";
……
<br><font color="#e78608">------解决方案--------------------</font><br>在 replace 中使用了 e 修正符,则字符串(或者是数组中的字符串)会被当作php代码
<br><font color="#e78608">------解决方案--------------------</font><br>
例<br>echo preg_replace('/(\d+),(\d+)/e', '$1+$2', '2,3');<br>输出 5
<br><font color="#e78608">------解决方案--------------------</font><br>简单的说就是PHP会把 $replace的结果当成表达式计算 <div class="clear"></div>
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号