String Replace

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
t2birkey
Forum Commoner
Posts: 28
Joined: Mon Feb 09, 2009 8:41 pm

String Replace

Post 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. :D
Drachlen
Forum Contributor
Posts: 153
Joined: Fri Apr 25, 2003 1:16 am

Re: String Replace

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: String Replace

Post 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;
t2birkey
Forum Commoner
Posts: 28
Joined: Mon Feb 09, 2009 8:41 pm

Re: String Replace

Post 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.
t2birkey
Forum Commoner
Posts: 28
Joined: Mon Feb 09, 2009 8:41 pm

Re: String Replace

Post 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.";
}
 
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: String Replace

Post 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.
Post Reply