I've been using the below code fine for a quick and efficient function of adding BB codes in my site.
function replace($text) {
//User Emotions
$text = str_replace(":)", "<img src=\"smiles/cool.gif\">", $text);
//User formatting
$text = str_replace("[center]", "<center>", $text);
$text = str_replace("[/center]", "</center>", $text);
$text = str_replace("[colour=red]", "<font color = red>", $text);
$text = str_replace("[/colour]", "</font>", $text);
$text = str_replace("[colour=blue]", "<font color = blue>", $text);
$text = str_replace("[/colour]", "</font>", $text);
$text = str_replace("[colour=green]", "<font color = green>", $text);
$text = str_replace("[/colour]", "</font>", $text);
$text = str_replace("[colour=orange]", "<font color = orange>", $text);
$text = str_replace("[/colour]", "</font>", $text);
$text = str_replace("[colour=white]", "<font color = white>", $text);
$text = str_replace("[/colour]", "</font>", $text);
$text = str_replace("[colour=black]", "<font color = black>", $text);
$text = str_replace("[/colour]", "</font>", $text);
$text = str_replace("[colour=code]", "<font color = code>", $text);
$text = str_replace("[/colour]", "</font>", $text);
$text = str_replace("[b]", "<strong>", $text);
$text = str_replace("[/b]", "</strong>", $text);
$text = str_replace("[i]", "<i>", $text);
$text = str_replace("[/i]", "</i>", $text);
$text = str_replace("[u]", "<u>", $text);
$text = str_replace("[/u]", "</u>", $text);
$text = str_replace("[move]", "<marquee>", $text);
$text = str_replace("[/move]", "</marquee>", $text);
$text = str_replace("[img]", "<img border = \"0\" src = ", $text);
$text = str_replace("[/img]", ">", $text);
$text = str_replace("[code]", "<div id=code>", $text);
$text = str_replace("[/code]", "</div>", $text);
$text = str_replace(array("\r\n", "\n", "\r"), '<br />', $text);
//Racial Hatred Blocking
include("snippets/racial_violations.php");
return $text;
}
The question i wanted to ask is how would I go about checking if $text contained say:
- "foo"
- "yar"
- "bar"
By passing my text var to the function (in a similar to the way i've done it above), but not replacing anything like str_replace does. I want to then pass out an error from the function so i could use:
if($text_error == 1){ echo "text does not contain foo, yar or bar";}
else ( echo "text CONTAINS foo, yar or bar";}
$text_error would either be 0 or 1 and would be assigned this value if text contained one of the three specified words.
Hopefully I've explained this sufficiently!