AV1611 Posted October 2, 2008 Share Posted October 2, 2008 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 More sharing options...
trq Posted October 2, 2008 Share Posted October 2, 2008 You'll be much better of with str_ireplace(). Its amazing what you'll find in the manual. Link to comment https://forums.phpfreaks.com/topic/126714-solved-str_replace-vs-preg_replace/#findComment-655391 Share on other sites More sharing options...
JasonLewis Posted October 2, 2008 Share Posted October 2, 2008 Then just feed it arrays. $str = "There was a big frog which ate the moth. What of the beetle?"; $bad = array("frog","moth","beetle"); echo str_ireplace($bad, "***", $str); Link to comment https://forums.phpfreaks.com/topic/126714-solved-str_replace-vs-preg_replace/#findComment-655392 Share on other sites More sharing options...
AV1611 Posted October 2, 2008 Author Share Posted October 2, 2008 never heard of that command? is that new ? Link to comment https://forums.phpfreaks.com/topic/126714-solved-str_replace-vs-preg_replace/#findComment-655397 Share on other sites More sharing options...
AV1611 Posted October 2, 2008 Author Share Posted October 2, 2008 Ah... I see it's new in PHP5. I haven't done a case insensitive replace in a long time so I guess I didn't ever notice that. I really do try and read the manual Link to comment https://forums.phpfreaks.com/topic/126714-solved-str_replace-vs-preg_replace/#findComment-655398 Share on other sites More sharing options...
JasonLewis Posted October 2, 2008 Share Posted October 2, 2008 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 https://forums.phpfreaks.com/topic/126714-solved-str_replace-vs-preg_replace/#findComment-655400 Share on other sites More sharing options...
trq Posted October 2, 2008 Share Posted October 2, 2008 Ah... I see it's new in PHP5. Sorry, Ive never used anything older than php5 so.... Link to comment https://forums.phpfreaks.com/topic/126714-solved-str_replace-vs-preg_replace/#findComment-655413 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.