Preg Replace - E 修饰符
$strpost_a = preg_replace("/\[CallName]([^]]+)\[\/CallName\]/e", getInfo('\\1',"fullname"), $strpost_a);
我总是得到这样的回报:
Parse error: syntax error, unexpected T_STRING in C:\wamp\www\-site\files\index\stream.php(88) : regexp code on line 1
Fatal error: preg_replace() [<a href='function.preg-replace'>function.preg-replace</a>]: Failed evaluating code: James -LastName Removed- in C:\wamp\www\-site-\files\index\stream.php on line 88
$strpost_a = preg_replace("/\[CallName]([^]]+)\[\/CallName\]/e", getInfo('\\1',"fullname"), $strpost_a);
I always get this in return:
Parse error: syntax error, unexpected T_STRING in C:\wamp\www\-site\files\index\stream.php(88) : regexp code on line 1
Fatal error: preg_replace() [<a href='function.preg-replace'>function.preg-replace</a>]: Failed evaluating code: James -LastName Removed- in C:\wamp\www\-site-\files\index\stream.php on line 88
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
preg_replace
的第二个参数必须始终是字符串。使用/e
标志不会改变这一点。第二个参数应该是一个由代码组成的字符串:字符串
'getInfo("\\1","fullname")'
将then 进行评估。如果您不在此处将其括在引号中,则它不会由preg_replace
执行,而是提前执行。这就是您收到错误消息的原因。(有时更适合使用
preg_replace_callback
,但可能需要特定的getInfo_fullname
在你的情况下。)The second parameter to
preg_replace
must always be a string. Using the/e
flag doesn't change that. And the second parameter simply should be a string consisting of code:The string
'getInfo("\\1","fullname")'
will then be evaluated. If you do not enclose it in quotes here, it will not be executed bypreg_replace
, but beforehand. That's why you got the error message.(It's sometimes more suitable to use
preg_replace_callback
, but would likely require a specificgetInfo_fullname
in your case.)首先,您需要正确转义括号:
其次,当使用
e
修饰符时,您的替换仍然需要是字符串。然而,在反向引用被替换后,它是一个被评估为 PHP 代码的字符串。请尝试以下操作:查看文档以获取有关 e 修饰符的更多详细信息
First, you need to properly escape your brackets:
Second, when using the
e
modifier your replacement still needs to be a string. However it is a string that is evaluated as PHP code after backreferences are substituted. Try the following:Check out the docs for more detail on the e modifier