laravel如何对敏感词汇过滤?
发布时间:2022-04-07 16:12:17 所属栏目:语言 来源:互联网
导读:laravel框架怎样实现对敏感词汇过滤?在做开发的时候,有些需求需要对敏感词汇进行过滤,很多新手可能对此对如何实现这个功能不是很清楚,下面给大家分享一个对用户的签名,回复进行敏感词检测的实例,供大家参考学习。 通过 composer 进行安装: composer r
|
laravel框架怎样实现对敏感词汇过滤?在做开发的时候,有些需求需要对敏感词汇进行过滤,很多新手可能对此对如何实现这个功能不是很清楚,下面给大家分享一个对用户的签名,回复进行敏感词检测的实例,供大家参考学习。 通过 composer 进行安装: composer require lustre/php-dfa-sensitive 然后在 app 目录下创建 Services ,并添加 SensitiveWords.php <?php namespace AppServices; use DfaFilterSensitiveHelper; 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); 然后我们就可以在项目中,使用 SensitiveWords::getBadWord() 来获取文本中是否有敏感词。 $bad_word = SensitiveWords::getBadWord($content); if (!empty($bad_word)) { throw new Exception('包含敏感词:' . current($bad_word)); } 在 storage 目录下创建 dict 目录存放敏感词词库,bk.txt .....等等,这些词库都是我在网上下载的。 以上就是关于laravel实现敏感词汇过滤的代码,小编认为还是具有一定的参考价值的,有需要的朋友可以参考上文内容自己实现,希望本文对大家有帮助。 (编辑:云计算网_泰州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |


浙公网安备 33038102330476号