1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class BlackList |
4
|
|
|
* @link https://www.icy2003.com/ |
5
|
|
|
* @author icy2003 <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2017, icy2003 |
7
|
|
|
*/ |
8
|
|
|
namespace icy2003\php\ihelpers; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* 敏感词过滤 |
12
|
|
|
*/ |
13
|
|
|
class BlackList |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* 敏感词树 |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $_map = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* 添加一个敏感词 |
25
|
|
|
* |
26
|
|
|
* @param string|array $content |
27
|
|
|
* |
28
|
|
|
* @return static |
29
|
|
|
*/ |
30
|
|
|
public function add($content) |
31
|
|
|
{ |
32
|
|
|
$words = Strings::split($content); |
|
|
|
|
33
|
|
|
$temp = &$this->_map; |
34
|
|
|
foreach ($words as $word) { |
35
|
|
|
if (false === isset($temp[$word])) { |
36
|
|
|
$temp[$word] = []; |
37
|
|
|
} |
38
|
|
|
$temp = &$temp[$word]; |
39
|
|
|
} |
40
|
|
|
$temp['isFind'] = true; |
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* 删除一个敏感词 |
46
|
|
|
* |
47
|
|
|
* @param string|array $content |
48
|
|
|
* |
49
|
|
|
* @return static |
50
|
|
|
*/ |
51
|
|
|
public function del($content) |
52
|
|
|
{ |
53
|
|
|
$words = is_string($content) ? Strings::split($content) : $content; |
54
|
|
|
$temp = &$this->_map; |
55
|
|
|
foreach ($words as $word) { |
56
|
|
|
if (isset($temp[$word])) { |
57
|
|
|
$temp = &$temp[$word]; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
$temp['isFind'] = false; |
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* 从头开始检测一个字符串是否是敏感词 |
66
|
|
|
* |
67
|
|
|
* - 发现敏感词,则返回该词 |
68
|
|
|
* - 没有敏感词,则返回 false |
69
|
|
|
* |
70
|
|
|
* @param string|array $content |
71
|
|
|
* @param boolean $findMore |
72
|
|
|
* |
73
|
|
|
* @return string|boolean |
74
|
|
|
*/ |
75
|
|
|
protected function _isStart($content, $findMore = true) |
76
|
|
|
{ |
77
|
|
|
$words = is_string($content) ? Strings::split($content) : $content; |
78
|
|
|
$temp = &$this->_map; |
79
|
|
|
$string = ''; |
80
|
|
|
$isFind = false; |
81
|
|
|
foreach ($words as $word) { |
82
|
|
|
if (isset($temp[$word])) { |
83
|
|
|
$string .= $word; |
84
|
|
|
if (true === I::get($temp[$word], 'isFind')) { |
85
|
|
|
$isFind = true; |
86
|
|
|
if (true === $findMore) { |
87
|
|
|
$temp = &$temp[$word]; |
88
|
|
|
continue; |
89
|
|
|
} else { |
90
|
|
|
return $string; |
91
|
|
|
} |
92
|
|
|
} else { |
93
|
|
|
$isFind = false; |
94
|
|
|
$temp = &$temp[$word]; |
95
|
|
|
continue; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
return true === $isFind ? $string : false; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* 查找一段文本里所有敏感词 |
104
|
|
|
* |
105
|
|
|
* @param string|array $content |
106
|
|
|
* |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
public function find($content) |
110
|
|
|
{ |
111
|
|
|
$words = is_string($content) ? Strings::split($content) : $content; |
112
|
|
|
$strings = []; |
113
|
|
|
while (true) { |
114
|
|
|
if (empty($words)) { |
115
|
|
|
return $strings; |
116
|
|
|
} |
117
|
|
|
$string = $this->_isStart($words); |
118
|
|
|
if (is_string($string)) { |
119
|
|
|
$strings[] = $string; |
120
|
|
|
} |
121
|
|
|
array_shift($words); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* 返回替换敏感词后的文本 |
127
|
|
|
* |
128
|
|
|
* @param string|array $content |
129
|
|
|
* @param string $char |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public function replace($content, $char = '*') |
134
|
|
|
{ |
135
|
|
|
$words = $this->find($content); |
136
|
|
|
$content = implode('', $words); |
137
|
|
|
$array = []; |
138
|
|
|
foreach ($words as $word) { |
139
|
|
|
$array[$word] = Strings::repeat($char, Strings::length($word)); |
140
|
|
|
} |
141
|
|
|
return Strings::replace($content, $array); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* 返回敏感词树 |
146
|
|
|
* |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
|
|
public function toArray() |
150
|
|
|
{ |
151
|
|
|
return $this->_map; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* 导出敏感词树到 php 文件 |
156
|
|
|
* |
157
|
|
|
* @param string|null $file |
158
|
|
|
* |
159
|
|
|
* @return void |
160
|
|
|
*/ |
161
|
|
|
public function toPhp($file = null) |
162
|
|
|
{ |
163
|
|
|
null === $file && $file = __DIR__ . '/' . date('YmdHis') . '.php'; |
164
|
|
|
$local = new LocalFile(); |
|
|
|
|
165
|
|
|
$array = Arrays::export($this->toArray(), true); |
166
|
|
|
$local->putFileContent($file, <<<EOT |
167
|
|
|
<?php |
168
|
|
|
|
169
|
|
|
return {$array}; |
170
|
|
|
|
171
|
|
|
EOT); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|