PHP code example of guanhui07 / dfa-sensitive

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

    

guanhui07 / dfa-sensitive example snippets


     
    use DfaFilter\SensitiveHelper;

// 获取感词库索引数组
$wordData = array(
    '察象蚂',
    '拆迁灭',
    '车牌隐',
    '成人电',
    '成人卡通',
    ......
);

// get one helper
$handle = SensitiveHelper::init()->setTree($wordData);

// 获取感词库文件路径
$wordFilePath = 'tests/data/words.txt';

// get one helper
$handle = SensitiveHelper::init()->setTreeByFile($wordFilePath);

$handle = SensitiveHelper::init()->setStopWordList(['&', '*', '.'])->setTreeByFile($wordFilePath);

$handle = SensitiveHelper::init()->setIgnoreCase()->setTree(['Av', '赌球网'])

$islegal = $handle->islegal($content);

// 敏感词替换为*为例(会替换为相同字符长度的*)
$filterContent = $handle->replace($content, '*', true);

// 或敏感词替换为***为例
$filterContent = $handle->replace($content, '***');

$markedContent = $handle->mark($content, '<mark>', '</mark>');

// 获取内容中所有的敏感词
$sensitiveWordGroup = $handle->getBadWord($content);
// 仅且获取一个敏感词
$sensitiveWordGroup = $handle->getBadWord($content, 1);