wuxue107 / wordfilter
屏蔽词检查、过滤、提取
This package's canonical repository appears to be gone and the package has been frozen as a result.
1.0.1
2024-06-12 08:06 UTC
This package is auto-updated.
Last update: 2025-04-12 10:04:05 UTC
README
两种实现,TreeWordFilter和ArrayWordFilter,适用于不同的php环境
TreeWordFilter:适用常驻内存的php环境、swoole、cli
- ArrayWordFilter : 适用于Web环境,调用一两次就退出程序的。
安装
composer require wuxue107/wordfilter
使用方式
// 加载你要的单词列表
$keywords = [
'山羊',
'草地'
];
// 干扰因子
$interferenceChars = [' ', '&', '*', '/', '|', '@', '.', '^', '~', '$'];
if(PHP_SAPI === 'cli'){
$wordFilter = new \wuxue107\wordfilter\TreeWordFilter($keywords,$interferenceChars);
}else{
$wordFilter = new \wuxue107\wordfilter\ArrayWordFilter($keywords,$interferenceChars);
}
$content = '那边草地上,有一只山羊';
// '测试是否存在屏蔽词 bool:';
$bool = $wordFilter->test($content);
var_dump($bool);
// 捕获一个屏蔽词 string | null
$word = $wordFilter->matchOne($content);
var_dump($word);
// 捕获所有屏蔽词 array
$words = $wordFilter->matchAll($content);
var_dump($words);
// 给原句中的屏蔽词打上马赛克 string
// bug: ArrayWordFilter 的实现中如果内容中包含屏蔽词,$newContent,会把干扰因子字符也删掉
$newContent = $wordFilter->mosaic($content);
var_dump($newContent);
// 打印结果
bool(true)
string(6) "草地"
array(2) {
[0]=>
string(6) "草地"
[1]=>
string(6) "山羊"
}
string(23) "那边**上,有一只**"