当前位置:网站首页>PHP implements sensitive word filtering system "suggestions collection"
PHP implements sensitive word filtering system "suggestions collection"
2022-07-01 17:14:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
Code description
1、 Sensitive thesaurus maintenance update script :
reload_dict.php, Provides automatic dictionary update to trie-tree Documentation process
PHP
<?php
// Set memory
ini_set('memory_limit', '128M');
// Read sensitive word dictionary
$handle = fopen('dict.txt', 'r');
// Generate empty trie-tree-filter
$resTrie = trie_filter_new();
while(! feof($handle)) {
$item = trim(fgets($handle));
if (empty($item)) {
continue;
}
// Add sensitive words one by one trie-tree
trie_filter_store($resTrie, $item);
}
// Generate trie-tree file
$blackword_tree = 'blackword.tree';
trie_filter_save($resTrie, $blackword_tree);2、trie Tree objects get tool classes
FilterHelper.php, Provide access to trie-tree object , Avoid duplicate generation trie-tree Objects and guarantees tree File and sensitive thesaurus synchronization update
PHP
<?php
/**
* Filter assistant
*
* getResTrie Provide trie-tree object ;
* getFilterWords Extract the filtered string
*
* @author W.Y.P ([email protected])
*/
class FilterHelper
{
// trie-tree object
private static $_resTrie = null;
// Update time of dictionary tree
private static $_mtime = null;
/**
* Prevent initialization
*/
private function __construct() {}
/**
* Prevent cloning objects
*/
private function __clone() {}
/**
* Provide trie-tree object
*
* @param $tree_file Dictionary tree file path
* @param $new_mtime The update time of the dictionary tree at the current call
* @return null
*/
static public function getResTrie($tree_file, $new_mtime) {
if (is_null(self::$_mtime)) {
self::$_mtime = $new_mtime;
}
if (($new_mtime != self::$_mtime) || is_null(self::$_resTrie)) {
self::$_resTrie = trie_filter_load($tree_file);
self::$_mtime = $new_mtime;
// Output dictionary file overload time
echo date('Y-m-d H:i:s') . "\tdictionary reload success!\n";
}
return self::$_resTrie;
}
/**
* Extract the filtered sensitive words from the original string
*
* @param $str Original string
* @param $res 1-3 Express From the position 1 Start ,3 Character length
* @return array
*/
static public function getFilterWords($str, $res)
{
$result = array();
foreach ($res as $k => $v) {
$word = substr($str, $v[0], $v[1]);
if (!in_array($word, $result)) {
$result[] = $word;
}
}
return $result;
}
}3、 Provide external filtering HTTP Access interface
filter.php, Use swool, External submission filter interface access
PHP
<?php
// Set the maximum running memory of the script , Adjust according to the dictionary size
ini_set('memory_limit', '512M');
// Set time zone
date_default_timezone_set('Asia/Shanghai');
// Load the helper file
require_once('FilterHelper.php');
// http Service bound ip And port
$serv = new swoole_http_server("182.92.177.16", 9502);
/**
* Processing requests
*/
$serv->on('Request', function($request, $response) {
// receive get Request parameters
$content = isset($request->get['content']) ? $request->get['content']: '';
$result = '';
if (!empty($content)) {
// Dictionary tree file path , The default directory is
$tree_file = 'blackword.tree';
// Clear file state cache
clearstatcache();
// When getting a request , The modification time of the dictionary tree file
$new_mtime = filemtime($tree_file);
// Get the latest trie-tree object
$resTrie = FilterHelper::getResTrie($tree_file, $new_mtime);
// filtering
$arrRet = trie_filter_search_all($resTrie, $content);
// Extract the filtered sensitive words
$a_data = FilterHelper::getFilterWords($content, $arrRet);
$result = json_encode($a_data);
}
// Definition http Service information and response processing results
$response->cookie("User", "W.Y.P");
$response->header("X-Server", "W.Y.P WebServer(Unix) (Red-Hat/Linux)");
$response->header('Content-Type', 'Content-Type: text/html; charset=utf-8');
$response->end($result);
});
$serv->start();Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/130918.html Link to the original text :https://javaforall.cn
边栏推荐
- 判断一棵二叉树是否为平衡二叉树
- [mathematical modeling] [matlab] implementation of two-dimensional rectangular packing code
- [C language foundation] 12 strings
- 阿里云、追一科技抢滩对话式AI
- [Supplément linguistique c] déterminer quel jour est demain (date de demain)
- 深度优先遍历和广度优先遍历[通俗易懂]
- Gold, silver and four want to change jobs, so we should seize the time to make up
- In aks, use secret in CSI driver mount key vault
- Exclusive news: Alibaba cloud quietly launched RPA cloud computer and has opened cooperation with many RPA manufacturers
- (12) About time-consuming printing
猜你喜欢
![[flask introduction series] cookies and session](/img/2e/d50e0a032c4ec48935cb5df206a29b.png)
[flask introduction series] cookies and session

存在安全隐患 起亚召回部分K3新能源

Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022

SQL注入漏洞(Mysql与MSSQL特性)

字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化

多线程并发之CountDownLatch阻塞等待

整形数组合并【JS】

Detailed explanation of string's trim() and substring()
荣威 RX5 的「多一点」产品策略

Exclusive news: Alibaba cloud quietly launched RPA cloud computer and has opened cooperation with many RPA manufacturers
随机推荐
荣威 RX5 的「多一点」产品策略
越来越多地使用 SLO 来实现可观测性|DevOps
Alibaba cloud, Zhuoyi technology beach grabbing dialogue AI
China acetonitrile market forecast and strategic consulting research report (2022 Edition)
Research Report on development prediction and investment direction of nylon filament sewing thread in China (2022 Edition)
[wrung Ba wrung Ba is 20] [essay] why should I learn this in college?
SystemVerilog-结构体(二)
China PBAT resin Market Forecast and Strategic Research Report (2022 Edition)
Redis 分布式鎖
中国生物降解塑料市场预测与投资战略报告(2022版)
Research and investment strategy report of neutral protease industry in China (2022 Edition)
如何写出好代码 — 防御式编程指南
ACM MM 2022视频理解挑战赛视频分类赛道冠军AutoX团队技术分享
China carbon disulfide industry research and investment strategy report (2022 Edition)
China sorbitol Market Forecast and investment strategy report (2022 Edition)
Judge whether a binary tree is a balanced binary tree
China BMS battery management system Market Research Report (2022 Edition)
【C补充】【字符串】按日期排序显示一个月的日程
Free lottery | explore the future series of blind box digital copyright works of "abadou" will be launched on the whole network!
Pytest learning notes (13) -allure of allure Description () and @allure title()