Forum Moderators: coopster

Message Too Old, No Replies

Banned expletives

         

Patrick Taylor

11:43 am on May 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm using this to replace banned expletives with asterisks:


function language_filter($string) {
$obscenities = array(" expletive1 ", " expletive1", " expletive2 ", " expletive2");
foreach ($obscenities as $banned) {
if (stristr(trim($string), $banned)) {
$length = strlen($banned);
for ($i = 1; $i <= $length; $i++) {
$asterisk .= "*";
}
$string = eregi_replace($banned, $asterisk, trim($string));
$asterisk = "";
}
}
return $string;
}

$the_article = language_filter($the_article);

This works, but when a banned string is contained within a longer word, the asterisks are substituted, which I don't want.

Is there a way that I can specify that a banned string should only be subsituted when it appears standalone or at the beginning of a word? At present I've done this by putting a space at both ends of it and repeating it with a space only at the start, but that doesn't seem too elegant.

Regards,

Patrick

Stormfx

6:11 pm on May 1, 2005 (gmt 0)

10+ Year Member



That's because it's looking for ANY instance of the word. So the script would replace 'ass' in 'assume'. What you need to do is be more specific in your regex. There's a decent article on regex here:

[mkssoftware.com...]

Patrick Taylor

9:32 pm on May 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks. I was hoping there might be a simpler way - but that's an excellent link.

Patrick