开发者

Preg Replace - E Modifier

$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


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:

$strpost_a = preg_replace(
                 "/\[CallName]([^]]+)\[\/CallName\]/e",
                 'getInfo("\\1","fullname")',
                 $strpost_a
             );

The string 'getInfo("\\1","fullname")' will then be evaluated. If you do not enclose it in quotes here, it will not be executed by preg_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 specific getInfo_fullname in your case.)


First, you need to properly escape your brackets:

"/\[CallName\]([^]]+)\[\/CallName\]/e"

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:

"getInfo('\\1', 'fullname')"

Check out the docs for more detail on the e modifier

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜