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