Lustre.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. declare (strict_types = 1);
  3. namespace library\utils;
  4. // +----------------------------------------------------------------------
  5. // | 敏感词过滤类
  6. // +----------------------------------------------------------------------
  7. // | [ WE CAN DO IT MORE SIMPLE ]
  8. // +----------------------------------------------------------------------
  9. // | Copyright (c) 2018-2020 rights reserved.
  10. // +----------------------------------------------------------------------
  11. // | Author: TABLE ME
  12. // +----------------------------------------------------------------------
  13. // | Date: 2020-10-04 18:31
  14. // +----------------------------------------------------------------------
  15. use DfaFilter\SensitiveHelper;
  16. class Lustre{
  17. private $client=null;
  18. private $content="";
  19. private $mark = "*";
  20. private $marks = "***";
  21. /**
  22. * 构造函数
  23. * @param type $content
  24. */
  25. public function __construct($content){
  26. $this->content = empty($content)?"":$content;
  27. $wordFilePath = app()->getRootPath(). 'library/utils/lustre/words.txt';
  28. $this->client = SensitiveHelper::init()->setTreeByFile($wordFilePath);
  29. }
  30. /**
  31. * 追加敏感词
  32. * @param type $words
  33. * @return boolean
  34. */
  35. static public function putEndWords($words=""){
  36. if($words==""){
  37. return false;
  38. }
  39. $file = app()->getRootPath().'library/utils/lustre/words.txt';
  40. $byte = file_put_contents($file,PHP_EOL.$words,FILE_APPEND | LOCK_EX);
  41. if(intval($byte)>0){
  42. return true;
  43. }else{
  44. return false;
  45. }
  46. }
  47. /**
  48. * 获取敏感词数组列表
  49. * @return type
  50. */
  51. static public function getWordsAr(){
  52. $file = app()->getRootPath().'library/utils/lustre/words.txt';
  53. $content = file_get_contents($file);
  54. $contentAr = explode(PHP_EOL,$content);
  55. return $contentAr;
  56. }
  57. /**
  58. * 检测是否含有敏感词
  59. */
  60. public function checkWords(){
  61. return $this->client->islegal($this->content);
  62. }
  63. /**
  64. * 敏感词替换为*为例(会替换为相同字符长度的*)
  65. * @param type $type
  66. * @return type
  67. */
  68. public function markReplace($type=1){
  69. $filterContent = "";
  70. if($type==1){
  71. $filterContent = $this->client->replace($this->content,$this->mark, true);
  72. }else{
  73. $filterContent = $this->client->replace($this->content,$this->marks);
  74. }
  75. return $filterContent;
  76. }
  77. /**
  78. *
  79. * @param type $type
  80. * @return type
  81. */
  82. public function getBadWord($count=0){
  83. $sensitiveWordGroup = [];
  84. if($count==0){
  85. $sensitiveWordGroup = $this->client->getBadWord($this->content);
  86. }else{
  87. $sensitiveWordGroup = $this->client->getBadWord($this->content,1,$count);
  88. }
  89. return $sensitiveWordGroup;
  90. }
  91. }