简体   繁体   中英

PHP function to check for banned words

I have this function to check for banned words in my string. However, I cannot detect banned words if the spaces are removed - rendering the function useless. Can someone help me improve it to detect the words without spaces.

$string = "Hellomynameisuser.";
check_for_banned_words($string);

function check_for_banned_words($string){
    $badWords = array(
        "ban",
        "bad",
        "user",
        "pass",
        "stack",
        "name",
        "html"
    );
    $matches = array();
    $matchFound = preg_match_all(
        "/\b(" . implode($badWords,"|") . ")\b/i", 
        $string, 
        $matches
    );
    if($matchFound):
        $words = array_unique($matches[0]);
        echo("<ul>");
        foreach($words as $word):
            echo("<li>" . $word . "</li>");
        endforeach;
        echo("</ul>");
    endif;
}

\\b检测到单词边界,将其删除以进行常规匹配。

"/(" . implode("|", $badWords) . ")/i", 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM