Page 1 of 1
String Replace
Posted: Wed Feb 11, 2009 5:37 pm
by t2birkey
Code: Select all
$badwords = array ("badword1", "badword2", "badword3");
$userpost = "i like to say things like badword1 because blah and then more blah.";
How do I search for any of the words in $badwords array? I think there is a method that can do this. I know I could do a foreach loop but I would rather keep it simple and use the function which does this for me.

Re: String Replace
Posted: Wed Feb 11, 2009 6:22 pm
by Drachlen
Hello,
Here is a working solution to your problem:
Code: Select all
$badwords = array ("badword1", "badword2", "badword3");
$goodwords = array ("goodword1", "goodword2", "goodword3");
$userpost = "i like to say things like badword1 because blah and then more blah.";
$grated = str_replace($badwords, $goodwords, $userpost);
echo $grated;
Also note that this example is actually in the PHP manual for the str_replace function (
http://us3.php.net/str_replace )
Good luck.
Re: String Replace
Posted: Wed Feb 11, 2009 7:45 pm
by John Cartwright
Just a note, if you want all the badwords replaced with a single censory word, it is not neccesary to pass an array of replacements. Secondly, you should consider using the case insensitive str_ireplace()
Code: Select all
$userpost = "i like to say things like badword1 because blah and then more blah.";
$badwords = array ("badword1", "badword2", "badword3");
$grated = str_ireplace($badwords, 'smurf', $userpost);
echo $grated;
Re: String Replace
Posted: Wed Feb 11, 2009 8:05 pm
by t2birkey
Yes, thanks a bunch for the replies. I briefly skimmed the php doc and didn't notice that one did allow for arrays. I knew I had seen it and used it before. I just kept seeing functions that only allowed strings.. Thanks a bunch.
Re: String Replace
Posted: Wed Feb 11, 2009 8:13 pm
by t2birkey
Okay actually I remember why I didn't use the str_replace. I want to run an if statement to check if the string contains any of the badwords from the $badwords array. Note the code below does not work. The php manual says if the needle is not a string it is converted into an int. "If needle is not a string, it is converted to an integer and applied as the ordinal value of a character. "
Code: Select all
if(strstr($users_words, $badwords)) echo "a bad word was found";
Also note below is my current solution, although I think there might be a more efficient solution.
Code: Select all
foreach($badword as $bw){
if(strstr($users_words, $bw)) echo "a bad word was found.";
}
Re: String Replace
Posted: Wed Feb 11, 2009 8:23 pm
by John Cartwright
Code: Select all
$badwords = array(
'foobar',
'feebar',
'shoot'
);
$content = 'hello foobar';
if (preg_match('#'. implode('|', array_map('preg_quote', $badwords)) .'#i', $content)) {
echo 'bad words found';
}
If you want to see which words were actually caught, you can add a 3rd parameter $matches and use preg_match_all() instead.
Also, in your solution you still did not take case sensitity into consideration, as I have already mentioned. You need to use the string insentive alternatives.