<?php
if($_POST)
{
//獲取文件內容轉為數組兩種方法:
/*
$fcon = file_get_contents("./filter.txt");
$filter_word = explode("\n",$fcon);
*/
$filter_word = file("./filter.txt");
//$filter_word = array("test1","test2","test3","test4");
$str = $_POST["mess"];
for($i=0;$i<count($filter_word);$i++)
{
if(preg_match("/".(trim($filter_word[$i]))."/i",$str))
{
echo "<script>alert('您輸入的內容含非法內容,請重輸!');</script>";
echo "<a href='index.php'>返回</a>";
exit;
}
}
echo "您輸入的內容是:".$str;
}
?>
<h2>測試過濾是否生效:</h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p><textarea name="mess" cols="40" rows="4"></textarea></p>
<p><input type="submit" name="sub" value="send"></p>
</form>
摘自:adamboy的博客