Re: Question on explode and join.
| From: | Christopher Watson | Date: | Wed, 13 Sep 2006 17:30:03 +0000 |
| Subject: | Re: Question on explode and join. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-241739@lists.php.net to get a copy of this message | ||
Definitely look into preg_match or even preg_replace, instead of
tokenizing the string and rubbing it up against an array of your own.
Iterate on your array of bad words, and then use Regular Expressions
to selectively hunt and squash. You MAY be able to do it in one regex
operation by building a search pattern that covers everything. Don't
know how much the PCRE functions can swallow at once, but it's worth a
try.
Christopher Watson
Principal Architect
The International Variable Star Index (VSX)
http://vsx.aavso.org
On 9/13/06, Beauford <php-user@netscan.ca> wrote:
Hi, I have a form which I want to check for inappropriate words before it is posted. I have used explode to put the string into an array using a space as the delimiter and then I check it against another array that contains the inappropriate words. I then replace the inappropriate words with *'s and join the array back into a string. This works perfectly except for one thing. If the word in the string has a any kind of punctuation after it (period, comma) it won't be matched. So if moron is an inappropriate word then "you are a moron" works, but "you are a moron." won't. Any ideas? Thanks This is my code. function badwords($string) {$language = array(contains the inappropriate words);$words = explode(" ",$string); $count = count(explode(" ", $string));for($i = 0; $i < $count; $i++) { if(in_array(strtolower($words[$i]), $language)) { $words[$i] = "*****"; } }$newcomments = join(" ",$words);return $newcomments;}