laravel實現敏感詞匯過濾


最近項目有需求,要對用戶的簽名,回復進行敏感詞檢測,然后搜到了一個好用的擴展,分享給大家。

https://github.com/FireLustre/php-dfa-sensitive

通過 composer 進行安裝:

composer require lustre/php-dfa-sensitive

然后在 app 目錄下創建 Services ,並添加 SensitiveWords.php

<?php

namespace App\Services;

use DfaFilter\SensitiveHelper;

class SensitiveWords
{
    protected static $handle = null;

    private function __construct()
    {
    }

    private function __clone()
    {
    }

    /**
     * 獲取實例
     */
    public static function getInstance($word_path = [])
    {
        if (!self::$handle) {

            //默認的一些敏感詞庫
            $default_path = [
                storage_path('dict/bk.txt'),
                storage_path('dict/fd.txt'),
                storage_path('dict/ms.txt'),
                storage_path('dict/qt.txt'),
                storage_path('dict/sq.txt'),
                storage_path('dict/tf.txt'),
            ];

            $paths = array_merge($default_path, $word_path);

            self::$handle = SensitiveHelper::init();

            if (!empty($paths)) {
                foreach ($paths as $path) {
                    self::$handle->setTreeByFile($path);
                }
            }
        }
        return self::$handle;
    }

    /**
     * 檢測是否含有敏感詞
     */
    public static function isLegal($content)
    {
        return self::getInstance()->islegal($content);
    }

    /**
     * 敏感詞過濾
     */
    public static function replace($content, $replace_char = '', $repeat = false, $match_type = 1)
    {
        return self::getInstance()->replace($content, $replace_char, $repeat, $match_type);
    }

    /**
     * 標記敏感詞
     */
    public static function mark($content, $start_tag, $end_tag, $match_type = 1)
    {
        return self::getInstance()->mark($content, $start_tag, $end_tag, $match_type);
    }

    /**
     * 獲取文本中的敏感詞
     */
    public static function getBadWord($content, $match_type = 1, $word_num = 0)
    {
        return self::getInstance()->getBadWord($content, $match_type, $word_num);
    }
}

然后我們就可以在項目中,使用 SensitiveWords::getBadWord() 來獲取文本中是否有敏感詞。

$bad_word = SensitiveWords::getBadWord($content);
if (!empty($bad_word)) {
    throw new \Exception('包含敏感詞:' . current($bad_word));
}

在 storage 目錄下創建 dict 目錄存放敏感詞詞庫,bk.txt .....等等,這些詞庫都是我在網上下載的。

下載地址:

https://download.csdn.net/download/jkko123/12066066

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM