PHP code example of ggob / dfa

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

    

ggob / dfa example snippets




use ggob\dfa\Dfa;

$path = '敏感词汇文件路径';
$content = '这是待检测过滤的文本';
$sensitiveWords = ['词汇1','词汇2'];
$replaceStr = '***';


$DFA = Dfa::init();

// 文件加载词汇模式
$handle = $DFA->setTreeByFile($path);

// 数组加载词汇模式
$handle = $DFA->setTree($sensitiveWords);

$result = $handle->replace($content, $replaceStr);
dd($result);


// 缓存到Redis中直接使用即可
$handle = Dfa::init()->setTreeByFile($path);
$redis = new Redis();
$redis->set('DFA_handle',$handle);
$DFA_handle = $redis->get('DFA_handle');
$result = $DFA_handle->replace($content, $replaceStr);