Re: Question on explode and join.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



$clean = $input;
foreach($badwords as $badword){
  $clean = preg_replace('/(\\W)$badword(\\W)/ims', '\\1' .
str_repeat('*', strlen($badword)) . '\\2', $clean);
}

On Wed, September 13, 2006 11:51 am, Beauford 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;
> }
>


-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux