PHP code example of bvtvd / words-filter

1. Go to this page and download the library: Download bvtvd/words-filter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bvtvd / words-filter example snippets


use bvtvd\Filter;  
  
$text = '你的观点很新颖, 但是从历史的维度看, 还存在狭隘的内部缺陷! to be or not to be, that's a question!';
  
$dict = ['观点', '内部缺陷', 'be'];
  
# 基本使用
$filter = new Filter($text, $dict);
echo $filter->clean();
//output: 你的*很新颖, 但是从历史的维度看, 还存在狭隘的*! to * or not to *, that's a question!
  
  
# 自定义替换字符
$blocker = '@';
$filter->blocker($blocker);
echo $filter->clean();
//output: 你的@很新颖, 但是从历史的维度看, 还存在狭隘的@! to @ or not to @, that's a question!
  
# 也可以在初始化的时候直接设置替换字符
$filter = new Filter($text, $dict, $blocker);
  
#  设置文本
$filter->text($text);
# 设置敏感词
$fitler->dict($dict);

  
# 你也可以设置不用的替换模式
$filter->model('preg');     // ['default', 'preg'] 
// preg 模式会替换为对应字符长度的 blocker
  
# 检查是否包含敏感词
$filter->check()