str_replace 匹配后跟 space 或特殊字符

str_replace when matched and followed by space or special characters

我有一个功能可以去除脏话。

单词列表由1700个坏词组成。

我的问题是它被审查了

'badwords '

但不是

'badwords.' , 'badwords' and the like.

如果我选择在

之后删除space

$badword[$key] = $word;

而不是

$badword[$key] = $word." ";

那么我会有一个更大的问题,因为如果坏词是 CON 那么它会去掉一个词 CONSTANT

我的问题是,我如何去掉一个 WORD 后跟除 space 之外的特殊字符?

badword. badword# badword,

.

function badWordFilter($data)
{
    $wordlist = file_get_contents("badwordsnew.txt");
    $words = explode(",", $wordlist);


    $badword = array();
    $replacementword = array();


    foreach ($words as $key => $word) 
    {
       $badword[$key] = $word." ";
       $replacementword[$key] = addStars($word);
    }


    return str_ireplace($badword,$replacementword,$data);
}


function addStars($word) 
{
    $length = strlen($word);

    return "*" . substr($word, 1, 1) . str_repeat("*", $length - 2)." " ;
}

假设$data是一段需要删改的文字,badWordFilter()将return有不良词的文字作为*

function badWordFilter($data)
{
    $wordlist = file_get_contents("badwordsnew.txt");



    $words = explode(",", $wordlist);

    $specialCharacters = ["!","@","#","$","%","^","&","*","(",")","_","+",".",",",""];

    $dataList = explode(" ", $data);

    $output = "";

    foreach ($dataList as $check) 
    {
        $temp = $check;
        $doesContain = contains($check, $words);
        if($doesContain != false){
            foreach($specialCharacters as $character){
                if($check == $doesContain . $character || $check == $character . $doesContain ){
                    $temp = addStars($doesContain);
                }
            }
        }

        $output .= $temp . " ";
    }


    return $output;
}

function contains($str, array $arr)
{
    foreach($arr as $a) {
        if (stripos($str,$a) !== false) return $a;
    }
    return false;
}


function addStars($word) 
{
    $length = strlen($word);

    return "*" . substr($word, 1, 1) . str_repeat("*", $length - 2)." " ;
}

Sandbox

我可以在@maxchehab 回答的帮助下回答我自己的问题,但我不能宣布他的回答,因为它在某些地方有错误。我发布了这个答案,以便其他人在需要 BAD WORD FILTER 时可以使用此代码。

function badWordFinder($data)
{
    $data = " " . $data . " ";  //adding white space at the beginning and end of $data will help stripped bad words located at the begging and/or end.        

    $badwordlist = "bad,words,here,comma separated,no space before and after the word(s),multiple word is allowed"; //file_get_contents("badwordsnew.txt"); //
    $badwords = explode(",", $badwordlist);

    $capturedBadwords = array();


    foreach ($badwords as $bad) 
    {
        if(stripos($data, $bad))
        {
            array_push($capturedBadwords, $bad);
        }             
    }

    return badWordFilter($data, $capturedBadwords);
}


function badWordFilter($data, array $capturedBadwords)
{

    $specialCharacters = ["!","@","#","$","%","^","&","*","(",")","_","+",".",","," "];

    foreach ($specialCharacters as $endingAt) 
    {
      foreach ($capturedBadwords as $bad) 
      {
          $data = str_ireplace($bad.$endingAt, addStars($bad), $data);   
      }                  
    }

    return trim($data);
}  


function addStars($bad) 
{
    $length = strlen($bad);

    return "*" . substr($bad, 1, 1) . str_repeat("*", $length - 2)." ";
}


$str = 'i am bad words but i cant post it here because it is not allowed by the website some bad words# here with bad. ending in specia character but my code is badly strong so i can captured and striped those bad words.';



echo "$str<br><br>";

echo badWordFinder($str);