str_replace匹配后跟空格或特殊字符

时间:2017-05-14 19:20:01

标签: php str-replace

我有一个删除亵渎语言的工作功能。

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

我的问题是它审查了

  

' badwords'

但不是

  

' BADWORDS' ,'坏词'等等。

如果我选择在

之后删除空格
  

$ badword [$ key] = $ word;

而不是

  

$ badword [$ key] = $ word。" &#34 ;;

然后我会遇到更大的问题,因为如果坏词是 CON 那么它会删除一个单词 CONSTANT

我的问题是,除了空格之外,我怎样才能删除WORD后跟特殊字符?

  

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)." " ;
}

2 个答案:

答案 0 :(得分:2)

假设 public async Task<IActionResult> Details(string _AccId) { if (_AccId == null) { return NotFound(); } var accs = await _context.Accounts .Include(Cust => Cust.Customers) //To reflect the Name of the Customer to whom the account belongs .Include(Bal => Bal.Balances).OrderByDescending(Balances.Report_Date) //For relevant Sub-Table on Accounts' detail page to show the balances on in shape of Report_Date (but in a descending sorted manner) .SingleOrDefaultAsync(m => m.SrcSys == _SrcSys && m.CustId == _CustId && m.AccId == _AccId); return View(accs); } 是需要审核的文字,$data会将包含错误字词的文字作为badWordFilter()返回。

*

https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/sort-filter-page

答案 1 :(得分:0)

我能够在@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);