preg_replace 具有替换功能
如何将 preg_replace
与函数一起用作替换参数?我收到此代码错误。
function getInfo($id,$slot){
if(!$id){ return '<b>Error</b> Id Not Returned. Please contact [email protected] for more information.'; }
$mm = mysql_query("SELECT * FROM `users` WHERE `id`='".$id."'");
$mma = mysql_fetch_assoc($mm);
$p = $mma[$slot];
return $p;
//return $id; <- Debug (Returns ID given)
}
$post = preg_replace(
"/\[CallName]([^]]+)\[\/CallName\]/",
getInfo('\\1',"fullname"),
$post
);
How do you use preg_replace
with a function as the replacement parameter? I'm getting an error with this code.
function getInfo($id,$slot){
if(!$id){ return '<b>Error</b> Id Not Returned. Please contact [email protected] for more information.'; }
$mm = mysql_query("SELECT * FROM `users` WHERE `id`='".$id."'");
$mma = mysql_fetch_assoc($mm);
$p = $mma[$slot];
return $p;
//return $id; <- Debug (Returns ID given)
}
$post = preg_replace(
"/\[CallName]([^]]+)\[\/CallName\]/",
getInfo('\\1',"fullname"),
$post
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
e
修饰符现在是 已弃用,改为preg_replace_callback。使用示例:
The
e
modifer is now deprecated in favor of preg_replace_callback.Sample usage:
我认为您忘记了 preg_replace 函数中的
e
修饰符 (PREG_REPLACE_EVAL),需要此修饰符来将替换字符串评估为 PHP 代码。它应该是这样的:请参阅本手册细节。
I think you forgot
e
modifier (PREG_REPLACE_EVAL) in preg_replace function, this modifier is needed to evaluate replacement string as PHP code. It should be like this:See this manual for for details.