Jump to content

preg_replace($bad, $good, $message)


Recommended Posts

using preg_replace i was trying to replace

$msg=[b]Bold[/b] Normal text[u]Underline[/u] Normal text

so of course i ran $msg threw the values

$bad = array("[b]","[/b]","[u]","[/u]","[i]","[/i]","[quote]","[quote]");$good = array("<b>","</b>","<u>","</u>","<i>","</i>","<div style=\"background-color:#99CCFF; width:100%\">","</div>");$msg=preg_replace($bad, $good, $msg);

but not it puts out[]Bold[/] Normal text []Underlne[/] Normal textwhats the crack with the business?

Link to comment
Share on other sites

You are forgetting that you are using regular expressions in the $bad variable. That is, each value is a regular expression. For those BBCodes only, str_replace() will do a better and more efficient job.If you still want to use preg_replace(), then you must escape all special characters in regular expressions from the $bad array. Those include "[", "]", and I think "/" too:

$bad = array("\[b\]","\[\/b\]","\[u\]","\[\/u\]","\[i\]","\[\/i\]","\[quote\]","[\/quote]");

(I also fixed the missing "/" at the last array member. That could have had an effect as well)

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...