expalmer/php-bad-words

PHP不良词过滤。检查字符串中是否存在不良词,同时也检查单独的词或字符串中的词

0.1.3 2015-06-25 20:39 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:37:30 UTC


README

PHP BAD WORDS是一个PHP包,当它在文本中找到不良词时返回TRUE或FALSE。

酷的地方在于你可以在字典的每个词中设置规则。

说明

  1. 单独:如果文本是你是混蛋,且字典中的不良词为array( '混蛋' );则不匹配,因为词混蛋必须是单独出现的。
  2. 其中:相同的文本你是混蛋和相同的词,但现在是一个有规则的数组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