Ryan S wrote:
Hey!
I'm just trying to replace some of the more bad words with their slightly censored counterparts like so
$bad_words = array(/*Well you know the words so am not going to write them here*/);
$bad_words_replacements = array("f*ck", "f*cking");
$comment = str_replace("$bad_words",$bad_words_replacements, $comment);
My question is this, for just two words its fine to use the above, but a pal tells me that if using a lot of words (eg: 15) and the $comment is big then it can take quite some time and be a bit of a processing strain as well because php first checks the first word from the good list against all the 15 words in the bad list against the comment then moves to the second word etc.
The above isn't fine since it will never work. The reason for that is
the fact that you enclose your array in quotes. This is something I've
never understood, I've seen tons of people do it, and to me it looks
like someone doesn't have a CLUE what he's doing. Why am I ranting about
this? This is why:
$some_string = 'this used to be an a, but is now also a b';
$bad_words = array('a','b');
echo str_replace("$bad_words", 'something else', $some_string);
What does this mean ? If you remove all variables, you'll get:
echo str_replace('array', 'O', 'this used to be an a, but is now also a b');
And the result you'll get:
this used to be an a, but is now also a b
However, leaving away the quotes, since they're useless around a single
variable anyway (what, don't you think PHP knows the variable is a
string without you adding quotes around it?), like so:
echo str_replace(array('a','b'), 'O', 'this used to be an a, but is now
also a b');
you'll get:
this used to Oe On O, Out is now Olso O O
Hmm, there's a difference! :o
- Tul
P.S. IMO people should never write "$var", if they REALLY want to cast
it to a string, do it explicitly like so: (string) $var; it keeps things
obvious and simple, not to mention working.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php