ejaboneta Posted February 16, 2011 Share Posted February 16, 2011 I am trying to add some quick tagging features to my site where you put in a code like "[img:1]" and my function will translate that into an image with an id of 1. I though I could put the whole page in a variable and replace anywhere that matched the pattern. When I try to use a switch() inside the function that replaces the preg_replace matches, the variables I expect to be there, aren't there. It isn't working because the variable "IMG" and "1" show up as "$1" and "$2" to the code. Is there another way to do this? function gCodes($code,$var) { switch($code) { case "IMG": $img = getImage($var); $return = "<a href=\"$img[location]\"><img src=\"$img[location]\" alt=\"$img[title]\"/></a>"; break; default: $return = "$code=>$var<br>Not Working<br>"; break; } return $return; } $text = "[img:1]"; $content = preg_replace("/\[([A-Z]{3})[a-zA-Z0-9]+)\]/",gCodes("$1","$2"),$text); echo $content; The above example prints out: IMG=>1 Not Working Link to comment https://forums.phpfreaks.com/topic/227843-switch-inside-preg_replace/ Share on other sites More sharing options...
ejaboneta Posted February 16, 2011 Author Share Posted February 16, 2011 Quote I am trying to add some quick tagging features to my site where you put in a code like "[img:1]" and my function will translate that into an image with an id of 1. I though I could put the whole page in a variable and replace anywhere that matched the pattern. When I try to use a switch() inside the function that replaces the preg_replace matches, the variables I expect to be there, aren't there. It isn't working because the variable "IMG" and "1" show up as "$1" and "$2" to the code. Is there another way to do this? function gCodes($code,$var) { switch($code) { case "IMG": $img = getImage($var); $return = "<a href=\"$img[location]\"><img src=\"$img[location]\" alt=\"$img[title]\"/></a>"; break; default: $return = "$code=>$var<br>Not Working<br>"; break; } return $return; } $text = "[img:1]"; $content = preg_replace("/\[([A-Z]{3})[a-zA-Z0-9]+)\]/",gCodes("$1","$2"),$text); echo $content; The above example prints out: IMG=>1 Not Working Even though $code prints out as "IMG", the switch doesn't see it as such. Oh and sorry, I didn't mean to reply to my post, I meant to modify it and I don't know how to delete it(if I can). Link to comment https://forums.phpfreaks.com/topic/227843-switch-inside-preg_replace/#findComment-1174886 Share on other sites More sharing options...
ansharma Posted February 16, 2011 Share Posted February 16, 2011 gCodes("$1","$2") change the variable name $1 and $2 because a variable name must start with a character. try, Link to comment https://forums.phpfreaks.com/topic/227843-switch-inside-preg_replace/#findComment-1174891 Share on other sites More sharing options...
sasa Posted February 16, 2011 Share Posted February 16, 2011 try <?php function gCodes($array) { $code = $array[1]; $var = $array[2]; switch($code) { case "IMG": $img = getImage($var); $return = "<a href=\"$img[location]\"><img src=\"$img[location]\" alt=\"$img[title]\"/></a>"; break; default: $return = "$code=>$var<br>Not Working<br>"; break; } return $return; } $text = "[img:1]"; $content = preg_replace_CALLBACK("/\[([A-Z]{3})[a-zA-Z0-9]+)\]/",'gCodes',$text); echo $content; ?> Link to comment https://forums.phpfreaks.com/topic/227843-switch-inside-preg_replace/#findComment-1174902 Share on other sites More sharing options...
ejaboneta Posted February 16, 2011 Author Share Posted February 16, 2011 Thank you sasa. That worked perfectly. Link to comment https://forums.phpfreaks.com/topic/227843-switch-inside-preg_replace/#findComment-1174904 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.