views:

33

answers:

3

Hi All,

Please excuse my noob-iness!

I have a $string, and would like to see if it contains any one or more of a group of words, words link c*t, fu*, sl** ETC. So I was thinking I could do:

if(stristr("$input", "dirtyword1"))
{
   $input = str_ireplace("$input", "thisWillReplaceDirtyWord");
}
elseif(stristr("$input", "dirtyWord1"))
{
   $input = str_ireplace("$input", "thisWillReplaceDirtyWord2");
}

...ETC. BUT, I don't want to have to keep doing if/elseif/elseif/elseif/elseif...

Can't I just do a switch statement OR have an array, and then simply say something like?:

$dirtywords = { "f***", "c***", w****", "bit**" };

if(stristr("$input", "$dirtywords"))
{
   $input = str_ireplace("$input", "thisWillReplaceDirtyWord");
}

I'd appreciate any help at all

Thank you

+3  A: 
$dirty = array("fuc...", "pis..", "suc..");
$censored = array("f***", "p***", "s***");

$input= str_ireplace($dirty, $censored , $input);

Note, that you don't have to check stristr() to do a str_ireplace()

killer_PL
I like your choice of words lol. Thanks! :D I will accept in 10mins when it lets me :)
lucifer
A: 

http://php.net/manual/en/function.str-ireplace.php

If search and replace are arrays, then str_ireplace() takes a value from each array and uses them to do search and replace on subject. If replace has fewer values than search, then an empty string is used for the rest of replacement values. If search is an array and replace is a string, then this replacement string is used for every value of search.

Mchl
+1  A: 

Surely not the best solution since I don't know too much PHP, but what about a loop ?

foreach (array("word1", "word2") as $word)
{
  if(stristr("$input", $word))
  {
     $input = str_ireplace("$input", $word" "thisWillReplaceDirtyWord");
  }
}

When you have several objects to test, think "loop" ;-)

Scharron
That's what I do in C# lol. Thanks! :)
lucifer