PHP code example of nickbai / sensitive

1. Go to this page and download the library: Download nickbai/sensitive 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/ */

    

nickbai / sensitive example snippets


$words=[
    '苹果',
    '香蕉'
];
$nodes =  Utils::words2Node($words);

//[
//    [
//        Node('苹', false),
//        Node('果', true)
//    ],
//    [
//        Node('香', false),
//        Node('蕉', true)
//    ],
//]

$tree = new Tree($nodes);
// $tree = new Tree();
// $tree->appendMultiple($nodes)

$sensitive = new SensitiveWords($tree);
//$sensitive = new SensitiveWords();
//$sensitive->setTree($tree);

$text = '苹果正好吃,我不爱吃香蕉, 香梨才是王道,香水有毒';
//如果文本包含敏感词,则返回包含的敏感词数组;否则返回false
$res = $sensitive->has($text);

//用指定字符,替换文本中敏感词
$string = $sensitive->filter($text,'#');

//敏感词数组;
$words=['苹果','香蕉'];

$nodes =  Utils::words2Node($words);
$tree = new Tree($nodes);
//如果敏感词数量比较大,字典树构建会比较慢,建议将构建好的字典树进行缓存
$sensitive = new SensitiveWords($tree);

$text = '苹果正好吃,我不爱吃香蕉, 香梨才是王道,香水有毒';
$res = $sensitive->has($text);
$string = $sensitive->filter($text,'#');