Jump to content

[SOLVED] str_replace vs. preg_replace


AV1611

Recommended Posts

ok,

 

I have written a shoutbox that seems to work ok so far.

 

Now I need to make a language filter.

 

I can come up with a word list but what I don't know how to do is make it not "case sensitive".

 

so

 

Frog

FROG

frOg

froG

 

are all the same.

 

Do I need to use preg_replace? I SUCK at regex...

 

Guidance?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/126714-solved-str_replace-vs-preg_replace/
Share on other sites

str_ireplace() was added in PHP5.

If you are not using PHP5 you could do it like this.

 

$str = "There was a big FrOg which ate the moTH. What of the BeETlE?";
$bad = array("/frog/i","/moth/i","/beetle/i");

echo preg_replace($bad, "***", $str);

//OR

$bad = array("frog","moth","beetle");
foreach($bad as $word){
$str = preg_replace("#$word#i", str_repeat("*", strlen($word)), $str);
}
echo $str;

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.