jcrowe / bad-word-filter
PHP 恶词过滤器。检查字符串或数组中是否存在恶词。
Requires
- php: >=4.0.0
- illuminate/support: >=4.0.0
Requires (Dev)
- mockery/mockery: 0.9.*
- phpunit/phpunit: 4.5.1
- satooshi/php-coveralls: dev-master
This package is auto-updated.
Last update: 2024-09-06 02:22:28 UTC
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 文件中
"jcrowe/bad-word-filter": "2.2.*"
- 运行 composer install
composer install
- 将 BadWordFilter 添加到您的 providers 数组,并在 app.php 中创建一个 facade 别名
$providers = array( ... ... 'JCrowe\BadWordFilter\Providers\BadWordFilterServiceProvider', ), $aliases = array( ... ... 'BadWordFilter' => 'JCrowe\BadWordFilter\Facades\BadWordFilter', ),
- 开始清理您的输入~
$cleanString = BadWordFilter::clean("my cheesy string"); var_dump($cleanString); // output: "my c****y string"
重要提示
BadWordFilter 无法也不应该防止 XSS 或 SQL 注入。请在代码中采取适当的步骤,在将用户输入存储到数据库或显示给客户端之前对其进行清理。
设置选项
BadWordFilter 有 4 个选项
$options = array( 'source' => 'file', 'source_file' => __DIR__ . '/bad_words.php', 'strictness' => 'very_strict', 'also_check' => array(), );
源类型
文件
如果您指定了 "文件" 类型的源类型,则还必须指定 source_file 或使用此包中包含的默认源文件。源文件必须返回一个要检查的单词数组。如果您想指定自定义恶词列表的严格级别,只需将数组拆分为主键 'permissive'、'lenient'、'strict'、'very_strict'、'strictest'、'misspellings' 即可。
数组
如果您指定了 "数组" 类型的源类型,则还必须指定 "bad_words_array" 键,该键包含要检查的单词列表。
严格性
可用选项包括:"permissive"、"lenient"、"strict"、"very_strict"、"strictest"、"misspellings"。
其中 "permissive" 将允许所有最差的单词通过,而 "strictest" 将尝试标记甚至 G 级别最高的单词。拼写错误也将检查常见的拼写错误和/或网络俚语。该仓库中 src/config/bad_words.php 文件中可以看到完整的单词列表。
也检查
除了在配置文件或数组中指定的默认列表之外,您还可以通过包含要标记的单词的 "also_check" 键来传递。
覆盖默认值
如果您将类用作实例或作为静态方法调用中的可选参数,则可以在构造函数中覆盖默认设置。
$myOptions = array('strictness' => 'permissive', 'also_check' => array('foobar')); $filter = new \JCrowe\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 = array('also_check' => array('cheesy')); $cleanString = BadWordFilter::clean("my cheesy string", '#!%^", $myOptions); var_dump($cleanString); // output: "my #!%^ string"
或
$myOptions = array('also_check' => array('cheesy')); $filter = new \JCrowe\BadWordFilter\BadWordFilter($myOptions); $cleanString = $filter->clean("my cheesy string", "#!$%"); var_dump($cleanString); // output: "my #!$% string"
如果您想在恶词周围放置任何内容(例如 HTML 标签)
$myOptions = array('also_check' => array('cheesy')); $filter = new \JCrowe\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 \JCrowe\BadWordFilter\BadWordFilter(); if ($filter->isDirty(array('this is a dirty string')) { /// do something }
clean
从字符串或数组中清理恶词。默认情况下,恶词将被替换为星号,但第一个和最后一个字母除外。您可以选择指定要替换的字符串参数: $input - 必需 - array|string $replaceWith - 可选 - string
返回: 清理后的数组或字符串
用法
$filter = new \JCrowe\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 \JCrowe\BadWordFilter\BadWordFilter(); if ($badWords = $filter->getDirtyWordsFromString("this really bad string")) { echo "You said these bad words: " . implode("<br />", $badWords); }
getDirtyKeysFromArray
在用 isDirty 方法检查数组后,可以使用此方法访问坏键参数: 无
返回: 字符串 - 数组键的点表示法
用法
$arrayToCheck = array( 'first' => array( 'bad' => array( 'a' => 'This is a bad string!', 'b' => 'This is a good string!', ), ), 'second' => 'bad bad bad string!', ); $filter = new \JCrowe\BadWordFilter\BadWordFilter(); if ($badKeys = $filter->getDirtyKeysFromArray($arrayToCheck)) { var_dump($badKeys); /* output: array( 0 => 'first.bad.a', 1 => 'second' ); */ }