tags:

views:

85

answers:

4

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!

+2  A: 

You can perhaps edit the replace() function to add in the beginning a check for any such words.

function replace($text) {

    $bannedWords = array('foo', 'yar', 'bar') ;
    foreach ($bannedWords as $bannedWord) {
        if (strpos($text, $bannedWord) !== false) {
            return false ;
        }
    }


    //..rest of str_replaces below here


    return $text
}

In this case if one of the words is found the function will return the boolean false. You can check for that wherever you are calling replace(). If none are found then the text will be returned after replacing any BBcode, as before.

So,

$replacedText = replace($text) ;
if ($replacedText === false) {
    //$text includes one of the bad words, act accordingly to inform the user
}
else {
    //$text was ok, and has its bbcode replaced, use it accordingly
}
Fanis
Thank you for the reply. I wanted to ask how i would check for say: "foo" and "Foo" without specifying both in the array? Also, how would i check if the function returned false?
Callum Johnson
`if(!replace($text)){`
Russell Dias
Seems it should return false if none of the words are in the string. Your function returns false if any of the words is in the string.
captaintokyo
@Callum if you want to check both foo and Foo you should use: `if (strpos(strtolower($text), $bannedWord) !== false)`
captaintokyo
@Callum Use http://www.php.net/manual/en/function.stripos.php or make `$textI = strtolower($text)` and then compare using that. I'm going to edit my answer to show you the false check
Fanis
@captaintokyo `(strpos($text, $bannedWord) !== false)` is true if `$bannedWord` has a position in `$text`, ie it exists in it. It's a bit counterintuitive but fast. It gives bool `FALSE` if it was not found.
Fanis
@Fanis, yeah, so if any of the words are in `$text` then the function `replace` returns `false`. I was wondering if that is what TS wants. It's not clear from his question.
captaintokyo
This seems to indicate that an error occurs if the words are not in the text: `if($text_error == 1){ echo "text does not contain foo, yar or bar";}`
captaintokyo
I wanted to display an error if the words are found in the text string.
Callum Johnson
A: 

Does the error occur if the text contains foo, yar or bar? Or does it occur when the text DOES NOT contain these words? Anyway, the function below returns TRUE if one of the words is in the string you pass to it and FALSE if none if the words are in it.

function test_string($var)
{
    $terms = array('foo', 'yar', 'bar');
    $exists = FALSE;
    foreach($terms as $term) { $exists = $exists || strpos($var, $term) !== FALSE; }
    return $exists;
}
captaintokyo
A: 

You can pass extra variable to function:

function replace($text, &$error) {

and set it later

$error = "text does not contain foo, yar or bar";
vladykx
+2  A: 

As for how to implement the above code more efficiently: str_replace also accepts arrays as its first two arguments:

$replace = array('[b]' => '<strong>', '[i]' => '<i>', ...);
$text = str_replace(array_keys($replace), array_values($replace), $text);
Marc Schütz