orionstar / bad-word-filter
无依赖的PHP不良词汇过滤器。检查字符串或数组中是否存在不良词汇。
README
为无依赖的PHP提供不良词汇过滤器。传入字符串或多维数组以检查预定义的不良词汇列表。使用应用程序附带列表或定义自己的自定义黑名单。BadWordFilter仅匹配整个单词(不包括符号),不匹配部分单词。这将匹配
$myString = "Don't be a #FOOBAR!"; $clean = BadWordFilter::clean($myString); var_dump($clean); // output: "Don't be a #F****R!"
但不会匹配
$myString = "I am an ASSociative professor"; $clean = BadWordFilter::clean($myString); var_dump($clean); // output: "I am an ASSociative professor"
快速入门指南
- 将以下内容添加到您的composer.json文件中
"orionstar/bad-word-filter": "3.*"
- 运行composer install
composer install
- 将BadWordFilter添加到您的使用列表中
use orionstar\BadWordFilter\BadWordFilter;
- 开始清理您的输入~
$cleanString = BadWordFilter::clean("my cheesy string"); var_dump($cleanString); // output: "my c****y string"
重要提示
BadWordFilter无法也不应该阻止XSS或SQL注入。请在将用户输入存储到数据库或显示给客户端之前在您的代码中采取适当的步骤进行清理。
设置选项
BadWordFilter接受3个选项
$options = [ 'source' => 'file', 'source_file' => __DIR__ . '/bad_words.php', 'also_check' => [], ];
源类型
文件
如果您指定了“文件”作为源类型,您必须还指定一个source_file或使用此包中包含的默认源文件。源文件必须返回一个要检查的单词数组。
数组
如果您指定了“数组”作为源类型,您还必须指定一个“bad_words_array”键,该键包含要检查的单词列表。
也要检查
除了在配置文件或数组中指定的默认列表外,您还可以传入一个“also_check”键,该键包含要标记的单词数组。
覆盖默认设置
如果使用类作为实例或作为静态方法调用的可选参数,您可以在构造函数中覆盖默认设置
$myOptions = ['also_check' => ['foobar']]; $filter = new \orionstar\BadWordFilter\BadWordFilter($myOptions); $cleanString = $filter->clean('Why did you FooBar my application?'); var_dump($cleanString); // output: "Why did you F****r my application?"
如何处理不良词汇
默认情况下,不良词汇将被替换为第一个字母后跟所需数量的星号,然后是最后一个字母。例如:“Cheese”将变成“C****e”
这可以通过传递新的字符串作为“clean”方法的参数来更改,以替换为指定的字符串
$myOptions = ['also_check' => ['cheesy']]; $cleanString = BadWordFilter::clean("my cheesy string", '#!%^", $myOptions); var_dump($cleanString); // output: "my #!%^ string"
或
$myOptions = ['also_check' => ['cheesy']]; $filter = new \orionstar\BadWordFilter\BadWordFilter($myOptions); $cleanString = $filter->clean("my cheesy string", "#!$%"); var_dump($cleanString); // output: "my #!$% string"
如果您想保留不良词汇并将其周围的内容(例如HTML标签)保留下来
$myOptions = ['also_check' => ['cheesy']]; $filter = new \orionstar\BadWordFilter\BadWordFilter($myOptions); $cleanString = $filter->clean("my cheesy string", '<span style="color: red;">$0</span>'); var_dump($cleanString); // output: "my <span style="color: red;">cheesy</span> string"
完整方法列表
isDirty
检查字符串或数组是否包含不良词汇参数:$input - 必需 - array|string
返回:布尔值
用法
$filter = new \orionstar\BadWordFilter\BadWordFilter(); if ($filter->isDirty(['this is a dirty string']) { /// do something }
clean
从字符串或数组中清除不良词汇。默认情况下,不良词汇将被替换为带星号的字符串,除了第一个和最后一个字母。您可以指定一个字符串来替换这些单词。参数:$input - 必需 - array|string $replaceWith - 可选 - string
返回:清理后的数组或字符串
用法
$filter = new \orionstar\BadWordFilter\BadWordFilter(); $string = "this really bad string"; $cleanString = $filter->clean($string);
STATIC clean
“clean”方法的静态包装。参数:$input - 必需 - array|string $replaceWith - 可选 - string $options - 可选 - array
返回:清理后的数组或字符串
用法
$string = "this really bad string"; $cleanString = BadWordFilter::clean($string);
getDirtyWordsFromString
返回匹配的不良词汇参数:$input - 必需 - string
返回:布尔值
用法
$filter = new \orionstar\BadWordFilter\BadWordFilter(); if ($badWords = $filter->getDirtyWordsFromString("this really bad string")) { echo "You said these bad words: " . implode("<br />", $badWords); }
getDirtyKeysFromArray
使用isDirty方法检查数组后,您可以使用此方法访问不良键参数:无
返回:字符串 - 数组键的点表示法
用法
$arrayToCheck = [ 'first' => [ 'bad' => [ 'a' => 'This is a bad string!', 'b' => 'This is a good string!', ], ], 'second' => 'bad bad bad string!', ]; $filter = new \orionstar\BadWordFilter\BadWordFilter(); if($badKeys = $filter->getDirtyKeysFromArray($arrayToCheck)) { var_dump($badKeys); /* output: array( 0 => 'first.bad.a', 1 => 'second' ); */ }