PHP implements filtering sensitive words in message messages
One code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>检测留言信息的敏感词</title> </head> <body> <table width="761" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#FEFEFE" bgcolor="#FFFFFF"> <form action="" method="post" name="form1" id="form1"> <tr> <td width="761" align="center" bgcolor="#F9F8EF"><table width="749" border="0" align="center" cellpadding="0" cellspacing="0" style="BORDER-COLLAPSE: collapse"> <tr> <td height="36" colspan="3" align="left" " bgcolor="#F9F8EF" > 姓 名: <input name="user_name" id="user_name" value=" 匿名" maxlength="64" type="text" /> <span style="COLOR: #ff0000">*</span></td> </tr> <tr> <td height="36" colspan="3" align="left" bgcolor="#F9F8EF"> 标 题: <input maxlength="64" size="30" name="title" type="text"/> <span style="COLOR: #ff0000">*</span></td> </tr> <tr> <td height="126" colspan="3" align="left" bgcolor="#F9F8EF"> 内 容: <textarea name="content" cols="60" rows="8" id="content" ></textarea> <span style="COLOR: #ff0000">*</span></td> </tr> <tr> <td height="40" colspan="3" align="center" bgcolor="#F9F8EF"><input name="submit" type="submit" class="btn1" id="submit" value="签写留言"/> <input name="reset" type="reset" class="btn1" value="清除留言" /></td> </tr> </table></td> </tr> <tr> <td width="761" align="center" bgcolor="#F9F8EF"><table width="749" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="703" height="31" align="center" > </td> </tr> </table></td> </tr> </form> </table> </body> </html> <?php if($_POST){ if (is_file("./filterwords.txt")){//判断给定文件名是否为一个正常的文件 $filter_word = file("./filterwords.txt");//把整个文件读入一个数组中 $str=$_POST['content']; for($i=0;$i<count($filter_word);$i++){//应用For循环语句对敏感词进行判断 if(preg_match("/".trim($filter_word[$i])."/i",$str)){//应用正则表达式,判断传递的留言信息中是否含有敏感词 echo "<script> alert('留言信息中包含敏感词!');history.back(-1);</script>"; exit; } } } } ?>
Two running results
Three code analysis
When the user submits a message, the program will use the regular expression preg_mctch() function to compare the message information with the sensitive words stored in the array to check whether the message submitted by the user contains Sensitive words. If the message contains sensitive words, a prompt message will pop up, otherwise the message information will be published successfully.
The "/i" in the preg_match() function means that letters are not case-sensitive when comparing sensitive words.
The above is the content of PHP's implementation of filtering sensitive words in message messages. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!
Related articles:
An efficient sensitive word filtering method (PHP)
Use PHP extension trie_filter to filter Chinese sensitive words
php sensitive word filtering uses a third-party extension trie_filter