【问题标题】:php bad words replacement with stars and output array用星星和输出数组替换php坏词
【发布时间】:2017-11-07 09:33:22
【问题描述】:

以下php 函数被用于用开始替换坏词,但我需要一个额外的参数来描述是否找到坏词。

    $badwords = array('dog', 'dala', 'bad3', 'ass');
    $text = 'This is a dog. . Grass. is good but ass is bad.';
    print_r( filterBadwords($text,$badwords));


    function filterBadwords($text, array $badwords, $replaceChar = '*') {
$repu = preg_replace_callback(array_map(function($w) { return '/\b' . preg_quote($w, '/') . '\b/i'; }, $badwords),
        function($match) use ($replaceChar) {
                    return str_repeat($replaceChar, strlen($match[0])); },
                    $text

                    );
 return array('error' =>'Match/No Match', 'text' => $repu );   
}// Func

如果找到的坏词应该像这样的输出

Array ([error] => Match[text] => Bad word dog match.)

如果没有找到坏词,那么

Array ([error] => No Match[text] => Bad word match.)

【问题讨论】:

  • 在什么基础上可以判定字符串是坏词?
  • 如果 $text 的任何单词与 $badwords 匹配,则表示该单词是错误的
  • 那么你遇到了什么问题?
  • 未能在“匹配/不匹配”的位置使用变量
  • 您最好的解决方案始终是手动审核。没有完美的自动化解决方案(参见“斯肯索普问题”)

标签: php arrays regex


【解决方案1】:

您可以使用以下内容:

function filterBadwords($text, array $badwords, $replaceChar = '*') {
   //new bool var to see if there was any match
    $matched = false;
    $repu = preg_replace_callback(array_map(
        function($w)
        {
            return '/\b' . preg_quote($w, '/') . '\b/i'; 
        }, $badwords),
        //pass the $matched by reference
        function($match) use ($replaceChar, &$matched)
        {
            //if the $match array is not empty update $matched to true
            if(!empty($match))
            {
                $matched = true;
            }
            return str_repeat($replaceChar, strlen($match[0]));
        }, $text);
    //return response based on the bool value of $matched
    if($matched)
    {
        $return = array('error' =>'Match', 'text' => $repu );
    }
    else
    {
        $return = array('error' =>'No Match', 'text' => $repu );
    }
    return $return;   
}

这使用referenceif 条件来查看是否有任何匹配,然后根据它返回响应。

输出(如果匹配):

array (size=2)
  'error' => string 'Match' (length=5)
  'text' => string 'This is a ***. . Grass. is good but *** is bad.'

输出(如果没有匹配):

array (size=2)
  'error' => string 'No Match' (length=8)
  'text' => string 'This is a . . Grass. is good but  is bad.'

【讨论】:

  • 哇。多么棒的解决方案
【解决方案2】:
<?php
$badwords = array('dog', 'dala', 'bad3', 'ass');
$text = 'This is a dog. . Grass. is good but ass is bad.';
$res=is_badword($badwords,$text);
echo "<pre>"; print_r($res);


function is_badword($badwords, $text) 
{    
    $res=array('No Error','No Match');
    foreach ($badwords as $name) {
        if (stripos($text, $name) !== FALSE) {
            $res=array($name,'Match');
            return $res;
        }
    }
    return $res;
}

?>

Output: 

Array
(
    [0] => dog
    [1] => Match
)

【讨论】:

    猜你喜欢
    • 2011-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    相关资源
    最近更新 更多