Da preg_match() schon erwähnt wurde, traue ich mich auch mal:
PHP-Code:
function has_badwords2(
$badwords = NULL, /// LIST of badwords
$haystack = NULL /// text to filter
) {
static $cb = NULL;
if (NULL === $cb) {
$cb = create_function('$badword', 'return preg_quote($badword, \'/\');');
}
$badwords = '/(' . implode('|', array_map($cb, $badwords)) . ')/';
return (bool) preg_match($badwords, $haystack);
}
Im Prinzip suchst du ja nach der Vorstufe eines Badword-Filters, oder?
Danach würde ich mal googlen -- vielleicht gibt's was Performanteres als das Zusammenkleben von Suchstrings.
*nachschieb*
Und hier noch die Variante mit preg_replace(). Sie nutzt ein Array als Filterliste:
PHP-Code:
function has_badwords(
$badwords = NULL, /// LIST of badwords
$haystack = NULL /// text to filter
) {
static $cb = NULL;
if (NULL === $cb) {
$cb = create_function(
'$badword',
'return \'/\A.*?\' . preg_quote($badword , \'/\') . \'.*\z/s\';'
);
}
$badwords = array_map($cb, $badwords);
$badwords[] = '/.*/';
preg_replace($badwords, '', $haystack, 1, $count);
return (bool) $count;
}
Fehlende Backslashes sind wie immer aus der Zitat-Ansicht zu kopieren.