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 support@site.com 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
);
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.
The
e
modifer is now deprecated in favor of preg_replace_callback.Sample usage: