过滤过敏字,首先是一个过敏字文件 dirtywords.txt;
里面的书写格式: 你好|不好|好的 中间用"|"隔开
我是在shopNC使用的:故而BasePath就是项目根目录常量;
ajaxMessage是自己写的一个作为json数据方法:
这个文件我建议放到根目录里,因为shopnc其他的程序的文件 都是具有权限的,可能打不开来,或者在控制器空进行调用的时候就是权限限制了;
里面的书写格式: 你好|不好|好的 中间用"|"隔开
我是在shopNC使用的:故而BasePath就是项目根目录常量;
ajaxMessage是自己写的一个作为json数据方法:
这个文件我建议放到根目录里,因为shopnc其他的程序的文件 都是具有权限的,可能打不开来,或者在控制器空进行调用的时候就是权限限制了;
在此的说一点,shopnc的中根目录 的常量 为BasePath,在进行文件操作时一定要加上:
/**
* 添加过敏字
*/
public function adddirwordsOp() {
$dirword=$_POST['dirwords'];
$filename=BasePath."/dirtyword.txt";
$result=file_put_contents($filename, $dirword);
if ($result===FALSE) {
$this->ajaxMessage(1,"添加失败",array());
}else{
$this->ajaxMessage(0,"修改成功",array());
}
}
/**
* 取出过敏字
*/
public function getdirwordsOp() {
$filename=BasePath."/dirtyword.txt";
$result=file_get_contents($filename,true);
echo $result;
}
/**
* @param string $str
* @return mixed
*进行过敏字过滤,将过敏字替换成***
*/
public function filter_word( $str )
{
$filename=BasePath."/dirtyword.txt";
if ( !($words = file_get_contents($filename,true)) ){
die('file read error!');
}
$str = strtolower($str);
$word = preg_replace("/[1,2,3]\r\n|\r\n/i", '', $words);
$matched = preg_replace('/'.$word.'/i', '***', $str);
return $matched;
}
/**
* ajax 返回信息处理
* @param number $code
* @param string $msg
* @param array $result
*/
public function ajaxMessage($code = 0, $msg = 'success', $result = array()) {
// todo 跨域检测
echo json_encode(array(
'code' => $code,
'msg' => $msg,
'result' => $result
));
}