0

I'm trying to update an older script. None of my preg_replace_callback variations are working, though. Does anyone know a solution?

This is the original code:

public static function filterHTML($html) {

    // a simple preg_replace will do for now to see it actually works
    // turn the PA::$config->profanity array into an array of regexp
    $profanity = array();
    foreach (PA::$config->profanity as $i => $w) {
        $profanity[] = "!\b(" . $w . ")\b!ie";
        // the \b ensures that we are dealing with szand alone 
        // occurences of the word or phrase
        // /ie case insensitive and run replacement as code
    }
    $repl_code = "ProfanityFilter::replace('$1')";
    return (preg_replace($profanity, $repl_code, $html));
}
チーズパン
  • 2,752
  • 8
  • 42
  • 63
  • Why nobody asks for a script to add profanities? – Casimir et Hippolyte Dec 20 '15 at 00:50
  • Your question is eventually a duplicate cause there are a lot of questions about how to replace the `e` modifier with `preg_replace_callback`. A better approach than using an array (if you always need the same replacement) is to build an alternation like `\b(prof1|prof2|...)\b` to parse the string only once. – Casimir et Hippolyte Dec 20 '15 at 01:01
  • *With an array, the string is parsed once for each item.* – Casimir et Hippolyte Dec 20 '15 at 01:07
  • http://banbuilder.com/ and similar things offering prebuilt "solutions" may be worth a look. Also http://www.codingforums.com/post-a-php-snippet/135849-super-simple-bad-word-filter.html (might need to replace short tags in this script) A short script http://forums.phpfreaks.com/topic/132754-solved-profanity-filter/ – Steve Dec 20 '15 at 06:42

0 Answers0