<?php
class
SensitiveWords
{
protected
$tree
= null;
protected
$callIsNumeric
= true;
public
function
__construct(
$path
= __DIR__ . '/sensitiveWords.txt')
{
$this
->tree =
new
WordNode();
$file
=
fopen
(
$path
,
"r"
);
while
(!
feof
(
$file
)) {
$words
= trim(
fgets
(
$file
));
if
(
$words
== '') {
continue
;
}
if
(
is_numeric
(
$words
)) {
$this
->callIsNumeric = false;
}
$this
->setTree(
$words
);
}
fclose(
$file
);
}
protected
function
setTree(
$words
)
{
$array
=
$this
->strToArr(
$words
);
$tree
=
$this
->tree;
$l
=
count
(
$array
) - 1;
foreach
(
$array
as
$k
=>
$item
) {
$tree
=
$tree
->getChildAlways(
$item
);
if
(
$l
==
$k
) {
$tree
->
end
= true;
}
}
}
public
function
check(
$str
)
{
$str
= trim(
str_replace
([' ',
"\n"
,
"\r"
], ['', '', ''],
$str
));
$ret
= [];
loop:
$strLen
=
strlen
(
$str
);
if
(
$strLen
=== 0) {
return
array_unique
(
$ret
);
}
if
(
$this
->callIsNumeric &&
is_numeric
(
$str
)) {
return
array_unique
(
$ret
);
}
$tree
=
$this
->tree;
$words
= '';
for
(
$i
= 0;
$i
<
$strLen
;
$i
++) {
$ord
= ord(
$str
[
$i
]);
if
(
$ord
<= 127) {
$word
=
$str
[
$i
];
}
elseif
(
$ord
<= 223) {
$word
=
$str
[
$i
] .
$str
[
$i
+ 1];
$i
+= 1;
}
elseif
(
$ord
<= 239) {
$word
=
$str
[
$i
] .
$str
[
$i
+ 1] .
$str
[
$i
+ 2];
$i
+= 2;
}
elseif
(
$ord
<= 244) {
$word
=
$str
[
$i
] .
$str
[
$i
+ 1] .
$str
[
$i
+ 2] .
$str
[
$i
+ 3];
$i
+= 3;
}
else
{
continue
;
}
$tree
=
$tree
->getChild(
$word
);
if
(
is_null
(
$tree
)) {
$str
=
substr
(
$str
,
$i
+ 1);
goto
loop;
}
else
{
$words
.=
$word
;
if
(
$tree
->
end
) {
$ret
[] =
$words
;
}
}
}
return
array_unique
(
$ret
);
}
protected
function
strToArr(
$str
)
{
$array
= [];
$strLen
= mb_strlen(
$str
);
for
(
$i
= 0;
$i
<
$strLen
;
$i
++) {
$array
[] = mb_substr(
$str
,
$i
, 1,
"utf8"
);
}
return
$array
;
}
}
class
WordNode
{
public
$end
= false;
protected
$child
= [];
public
function
getChildAlways(
$word
)
{
if
(!isset(
$this
->child[
$word
])) {
$this
->child[
$word
] =
new
self();
}
return
$this
->child[
$word
];
}
public
function
getChild(
$word
)
{
if
(
$word
=== '') {
return
null;
}
if
(isset(
$this
->child[
$word
])) {
return
$this
->child[
$word
];
}
return
null;
}
}