RE: [PHP] Question on explode and join.
| From: | Beauford | Date: | Wed, 13 Sep 2006 17:27:59 +0000 |
| Subject: | RE: [PHP] Question on explode and join. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-241740@lists.php.net to get a copy of this message | ||
There ya go. Works and is shorter than what I did. Thanks.
One other question. if I have bunny and bunnyhole in the badword array. if
someone types in bunnyhole, I end up getting *hole in the clean string. If I
change the order of the words in the array would that solve the problem?
Thanks
_____
From: Eric Butera [mailto:digital.tarsier@gmail.com]
Sent: September 13, 2006 1:07 PM
To: Beauford
Cc: php
Subject: Re: [PHP] Question on explode and join.
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;
}
Perhaps you could try an approach like this:
<?php
$dirty = array(
'ipsum',
'eloquentiam',
'Vero'
);
$string = "Lorem ipsum ius no etiam veniam, usu alii novum ne, sed cu
molestiae eloquentiam. Vero invenire philosophia est ne, quo nemore timeam
an.";
$clean = str_replace($dirty, '*', $string);
echo "<br>string: ". $string;
echo "<br>clean: ". $clean;
GENERATES:
string: Lorem ipsum ius no etiam veniam, usu alii novum ne, sed cu molestiae
eloquentiam. Vero invenire philosophia est ne, quo nemore timeam an.
clean: Lorem * ius no etiam veniam, usu alii novum ne, sed cu molestiae *. *
invenire philosophia est ne, quo nemore timeam an.