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
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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.