PHP检测敏感词,敏感词库整理

180it 2020-10-29 PM 2942℃ 0条
<?php

/**
 * 检测关键字
 * @param $content 需要检测的内容
 */
function checkKeyWords(String $content)
{
    $file_path = "keywords"; // 文件的绝对路径
    // 检测文件是否存在
    if (file_exists($file_path)) {
        $fp = fopen($file_path, "r"); // 以只读的方式打开
        $str = "";
        while (!feof($fp)) {
            $str .= fgets($fp); //逐行读取。如果fgets不写length参数,默认是读取1k。
        }
        fclose($fp); // 关闭文件
        if (false !== strstr($str, $content)) {
            echo "存在敏感词";
            die;
        }
    } else {
        echo "文件不存在";
        die;
    }
}

// 调用
$txt = "敏感词内容";
checkKeyWords($txt);
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

PHP检测敏感词,敏感词库整理