鍍金池/ 問答/PHP/ 關(guān)于php過濾垃圾評(píng)論

關(guān)于php過濾垃圾評(píng)論

寫了一個(gè)垃圾評(píng)論過濾插件,但是總覺得哪里有問題,請(qǐng)各位提提建議,代碼如下:
1.config.php

<?php
return [
    'processing_mode' => 1,
    'keyword'    => '去,的',    //填寫要過濾的關(guān)鍵字以英文,號(hào)分割
];

2.類文件

<?php
class MaliciousCommentsFiltering
{
    protected $config;
    public function __construct()
    {
        //引入配置文件
        $this->config=include 'config.php';
    }

    /**
     * 搜索關(guān)鍵字并替換
     * @param $searchKeyword 要搜索的關(guān)鍵字
     * @return mixed|string 返回處理結(jié)果
     */
    public function senseKey($searchKeyword)
    {
        $keyword = $this->config['keyword'];
        $keyword = explode(',', $keyword);
        $reslut  = '';
        foreach ($keyword as $value) {
            if (strpos($searchKeyword, $value) !== false) {
                //如果processing_mode設(shè)置為1代表,用*號(hào)代替關(guān)鍵字
                if ($this->config['processing_mode'] == 1) {
                    $reslut = str_ireplace($value, '***', $searchKeyword);
                } else {
                    //如果返回300代表存在關(guān)鍵字
                    $reslut = 300;
                }
            }
        }
        return $reslut;
    }
}

歡迎各位發(fā)表自己的觀點(diǎn)

回答
編輯回答
悶騷型

foreach循環(huán)如果匹配到2個(gè)以上的敏感詞,你的result變量只屏蔽了最后一個(gè)詞

2017年2月18日 08:33
編輯回答
寫榮

為什么不用 ac 自動(dòng)機(jī)……你要知道你這么做的誤傷和效率很可怕的

2017年8月10日 15:50
編輯回答
情皺

可以優(yōu)化下效率,比如一個(gè)位置命中了敏感詞,是否可以將這塊位置標(biāo)記為以匹配,別的關(guān)鍵字就略過此位置。

2018年4月6日 14:46
編輯回答
醉淸風(fēng)

用正則呢,以下是手冊(cè)上的:

$string = 'The quick brown fox jumps over the lazy dog.';
$patterns = array();
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements = array();
$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';
echo preg_replace($patterns, $replacements, $string);

輸出:
The bear black slow jumps over the lazy dog.

2018年5月29日 15:33