介绍
这篇文章给大家介绍使用laravel框架怎么过滤敏感词汇,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
通过作曲家进行安装:
composer require 光泽/php-dfa-sensitive
然后在应用目录下创建服务,并添加SensitiveWords。php
& lt; php ? namespace 应用\服务; use DfaFilter \ SensitiveHelper; class SensitiveWords { protected 才能;static  handle 美元;=,空; private 才能;function  __construct () {才能 ,,} private 才能;function  __clone () {才能 ,,} ,/* * ,,*,获取实例 ,,*/public 才能static function  getInstance ($ word_path =, []) {才能 ,,,if (! self:: $处理),{ ,,,,,//默认的一些敏感词库 ,,,,,default_path 美元;=,( ,,,,,,,storage_path (& # 39; dict/bk.txt& # 39;), ,,,,,,,storage_path (& # 39; dict/fd.txt& # 39;), ,,,,,,,storage_path (& # 39; dict/ms.txt& # 39;), ,,,,,,,storage_path (& # 39; dict/qt.txt& # 39;), ,,,,,,,storage_path (& # 39; dict/sq.txt& # 39;), ,,,,,,,storage_path (& # 39; dict/tf.txt& # 39;), ,,,,,); ,,,,,paths 美元;=,array_merge (default_path美元,,word_path美元); ,,,,,self:: $ handle =, SensitiveHelper: init (); ,,,,,if (!空(路径)美元),{ ,,,,,,,foreach (paths 美元;as $路径),{ ,,,,,,,,,自我:美元处理→setTreeByFile(路径); ,,,,,,,} ,,,,,} ,,,} ,,,return 自我::$处理; ,,} ,/* * ,,*,检测是否含有敏感词 ,,*/public 才能static function  isLegal(内容) {才能 ,,,return 自我:getInstance ()→islegal($内容); ,,} ,/* * ,,*,敏感词过滤 ,,*/public 才能;static  function 取代(内容、美元,美元replace_char =, & # 39; & # 39;,, repeat 美元;=,假,,match_type 美元;=,1) {才能 ,,,return 自我:getInstance()→替换(内容、美元,replace_char美元,重复,美元,美元match_type); ,,} ,/* * ,,*,标记敏感词 ,,*/public 才能;static  function 马克(内容、美元,看到start_tag美元,end_tag美元,美元match_type =, 1) {才能 ,,,return 自我:getInstance()→马克(内容、美元,看到start_tag美元,end_tag美元,美元match_type); ,,} ,/* * ,,*,获取文本中的敏感词 ,,*/public 才能static function  getBadWord(内容、美元,美元match_type =, 1, $ word_num =, 0) {才能 ,,,return 自我:getInstance ()→getBadWord(内容、美元,match_type美元,美元word_num); ,,} }
然后我们就可以在项目中,使用<代码> SensitiveWords:: getBadWord() 代码>来获取文本中是否有敏感词。
bad_word 美元;=,SensitiveWords: getBadWord($内容); if (!空(bad_word美元)),{ throw 才能;new  \异常(& # 39;包含敏感词:& # 39;,只当前(bad_word美元)); }
关于使用laravel框架怎么过滤敏感词汇就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看的到。