Warning:preg_match() [function.preg-match]:Compilation failed:nothing to repeat解決方案
來源:互聯網
上載者:User
Warning:preg_match() [function.preg-match]:Compilation failed:nothing to repeat
Warning:preg_match() [function.preg-match]:Compilation failed:nothing to repeat at offset 0 in E:\EasyPHP-5.3.4.0\www\01\note_check.php on line 9 怎麼修改,望各高手幫幫我啊?
程式如下:
error_reporting(E_ALL ^ E_NOTICE);
require("global.php");
if($_POST){
if (is_file("./filterwords.txt")){//判斷給定檔案名稱是否為一個正常的檔案
$filter_word = file("./filterwords.txt");//把整個檔案讀入一個數組中
$str=$_POST['content'];
for($i=0;$i if(preg_match("/".trim($filter_word[$i])."/i",$str)){//應用Regex,判斷傳遞的留言資訊中是否含有敏感詞
echo "";
exit;
}
}
}
$content =$_POST['content'];
$user_name = $_POST['user_name'];
$title = $_POST['title'];
$mood= $_GET['mood'];
$head = $_POST['head'].".gif";
$note_flag=$_POST['checkbox'];
if($note_flag!=1){$note_flag=0;}
$datetime=date("Y-m-d H:i:s");
$sql = "insert into tb_note (note_user,note_title,note_content,note_mood,note_time,note_user_pic,note_flag) values('".$user_name."','".$title."','".$content."','".$mood."','".$datetime."','".$head."','".$note_flag."')";
$DB->query($sql);
$url = "./index.php";
redirect_once($url);
}
?>
------解決方案--------------------
你的 $filter_word[$i] 中包含了 perl 保留字,尤其是 ?、+、*
需要轉義
正確的寫法是
preg_match("/".preg_quote(trim($filter_word[$i]))."/i",$str)