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 Quote 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 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). Quote 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, Quote 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; ?> Quote 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. Quote Link to comment https://forums.phpfreaks.com/topic/227843-switch-inside-preg_replace/#findComment-1174904 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.