if (!function_exists('checkWords')) {
/**
* 检查敏感词
* @param $list
* @param $str
* @return string
*/
function checkWords($list, $str, $flag = false)
{
$count = 0; //违规词的个数
$sensitiveWord = ''; //违规词
$stringAfter = $str; //替换后的内容
$pattern = "/" . implode("|", $list) . "/i"; //定义正则表达式
if (preg_match_all($pattern, $str, $matches)) { //匹配到了结果
$patternList = $matches[0]; //匹配到的数组
$count = count($patternList);
$sensitiveWord = implode(',', $patternList); //敏感词数组转字符串
// $replaceArray = array_combine($patternList, array_fill(0, count($patternList), '***')); //把匹配到的数组进行合并,替换使用
// $stringAfter = strtr($str, $replaceArray); //结果替换
// 临时解决方案
$itemArr = [];
if (!empty($patternList)) {
foreach ($patternList as $val) {
if (!$val) {
continue;
}
$itemArr[] = str_pad("", mb_strlen($val), "*", STR_PAD_LEFT);
}
}
$replaceArray = array_combine($patternList, $itemArr); //把匹配到的数组进行合并,替换使用
$stringAfter = strtr($str, $replaceArray); //结果替换
}
$log = "原句为 [ {$str} ]<br/>";
if ($count == 0) {
$log .= "暂未匹配到敏感词!";
} else {
$log .= "匹配到 [ {$count} ]个敏感词:[ {$sensitiveWord} ]<br/>" .
"替换后为:[ {$stringAfter} ]";
}
if (!$flag) {
return $stringAfter;
} else {
return $count;
}
}
}
敏感词检查函数
相关推荐
-
生成图片
from PIL import Image, ImageColor, ImageDraw, ImageFont, ImageFilterdef create_image_with_text(size, color, text, font_path, font_size, text_color, shadow_color, output_path): """ Create a new image of specified size and color with centered text that has a border and shadow. :param size: A tuple con
-
获取指定目录下的所有图片信息
1 获取指定目录下的所有图片信息// 获取指定目录下的所有图片信息 public function getImagesInfo($directory) { $images = []; // 创建递归目录迭代器 $iterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY ); // 遍历目录中的每个文件 foreach (
-
Thinkphp各版本的PHP要求
ThinkPHP 8.0:运行环境要求PHP8.0+,兼容PHP8.3ThinkPHP 6.1:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 6.0:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 5.1:运行环境要求PHP5.6+,兼容PHP8.0ThinkPHP 5.0:运行环境要求PHP5.4+,兼容PHP7.3