tscheckenbach / php-bad-words
PHP 不良词过滤。检查字符串中是否存在不良词,同时检查单独的词或词组
0.1.3
2015-06-25 20:39 UTC
Requires
- php: >=5.3.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-20 17:21:54 UTC
README
PHP BAD WORDS 是一个PHP包,当在文本中找到不良词时返回TRUE或FALSE。
酷的地方是你可以为字典中的每个词设置规则。
说明
- 单独:如果文本是
你是个混蛋,并且字典中包含不良词array( '混蛋' );它不会匹配,因为单词混蛋必须单独出现。 - 词组:同样的文本
你是个混蛋和同样的单词,但现在是一个包含规则的数组array( array('混蛋','词组') );,现在它匹配了,因为规则词组。它将通过单词混蛋查找文本中的每个单词,并且单词混蛋包含单词混蛋,明白了吗?
安装
要安装,请将其包含在项目的 composer.json 文件中。
"expalmer/php-bad-words": "dev-master",
此包不需要任何其他依赖项即可工作。
用法
/* Using Composer */ require_once 'vendor/autoload.php'; use \Expalmer\PhpBadWords\PhpBadWords as BadWords; $myDictionary = array( array("ass","among"), "anal", "butt" ); $myText = "You are an asshole"; $badwords = new BadWords(); $badwords->setDictionaryFromArray( $myDictionary ) ->setText( $myText ); var_dump( $badwords->check() ); // output: TRUE. // Because the dictionary has a word `ass` with `among` rule. var_dump( $badwords->checkAlone() ); // output: FALSE. // Because now it verified by word `ass` alone, and it does not exist alone. // ( It ignores dictionary word rules ) var_dump( $badwords->checkAmong() ); // output: TRUE. // Again, it verified by `ass` among each word in the text. // ( It ignores dictionary word rules ) // WITH THE WORD `anal` $myAnotherText = "She is not a research analyst"; $badwords->setText( myAnotherText ); var_dump( $badwords->check() ); // output: FALSE. var_dump( $badwords->checkAlone() ); // output: FALSE. var_dump( $badwords->checkAmong() ); // output: TRUE.
设置字典
字典的样子。
array( array("ass","among"), // rule: among "anal", // rule: alone "beastiality", // rule: alone "fart", // rule: alone array("vag","among") // rule: among );
数组形式
$myDictionary = array( array("ass","among"), "butt" ); $bad = new PhpBadWords(); $obj->setDictionaryFromArray( $myDictionary );
文件形式
/* // dictionary.php <?php return array( array("ass","among"), "anal", "butt" ); */ $bad = new PhpBadWords(); $obj->setDictionaryFromFile( __DIR__ . "/dictionary.php" );
测试
要运行此包的单元测试,只需从包目录运行 vendor/bin/phpunit。