正则的模式修正符“e”的作用解决方法

WBOY
Release: 2016-06-13 13:41:45
Original
1114 people have browsed it

正则的模式修正符“e”的作用
我记得学php的时候,老师说“e”是配合preg_replace($reg, $replace, $text)。使替换的内容($replace),可以使用正则$reg中的子模式,但以下这个代码却报错:

PHP code
<!--

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 ','

Copy after login


把正则中的模式修正符e删除了,就没事了,这是怎么回事?
模式修正符e到底是什么作用?

------解决方案--------------------
PHP code

……
$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>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template