1、在thinkphp内读取文件
在tp内读取文件的默人访问的路径是根目录同级的路径
这是controoler内的写法:
class ValidateController{
static function checkSensitiveWord($content){
$file = fopen('./sensitiveWord','r');
if(!$file){
$sensitiveWords = [];
}else{
$word = [];
$i = 0;
while (!feof($file)){
$word[$i] = fgets($file);
$i++ ;
}
fclose($file);
$sensitiveWords = array_filter($word);
}
}
这是文件sensitiveWord的位置:
和入口文件index.php同级。不过这边的文件是以’换行’\n的方式来读取的,如果是采用别的方式,自行读取解析就好了
2、判断是否含有敏感词汇
$count = count($sensitiveWords);
for ($i = 0; $i<$count; $i++){
if(substr_count($content,$sensitiveWords[$i])>0){
return false;
}else{
if($count == ($i + 1)){
return true;
}
}
}
如果false的话,说明待检测的变量content中含有敏感词汇。