您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP使用正则表达式实现过滤非法字符串功能示例

2024/2/25 8:22:17发布15次查看
这篇文章主要介绍了php使用正则表达式实现过滤非法字符串功能,结合留言板数据提交功能示例分析了php使用正则表达式preg_replace函数进行字符串正则替换相关操作技巧,需要的朋友可以参考下
本文实例讲述了php使用正则表达式实现过滤非法字符串功能。分享给大家供大家参考,具体如下:
一、代码
1、index.php
<!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><style type="text/css"><!--body { margin-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px;}--></style></head><body><table width="1002" height="585" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="379" height="226"> </td> <td width="445"> </td> <td width="178"> </td> </tr> <form id="form1" name="form1" method="post" action="index_ok.php"> <tr> <td height="260"> </td> <td align="center" valign="top"><table width="430" border="1" cellpadding="1" cellspacing="1" bordercolor="#ffffff" bgcolor="#99cc67"> <tr> <td width="81" height="30" align="right" bgcolor="#ffffff">发布主题:</td> <td width="307" align="left" bgcolor="#ffffff"><input name="title" type="text" id="title" size="30" /></td> </tr> <tr> <td align="right" bgcolor="#ffffff">发布内容:</td> <td align="left" bgcolor="#ffffff"><textarea name="content" cols="43" rows="13" id="content"></textarea></td> </tr> </table></td> <td> </td> </tr> <tr> <td height="99"> </td> <td align="center" valign="top"><table width="315" height="37" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="169" align="center"><input type="image" name="imagefield" src="images/bg1.jpg" /></td> <td width="146" align="center"><input type="image" name="imagefield2" src="images/bg3.jpg" onclick="form.reset();return false;" /></td> </tr> </table></td> <td> </td> </tr> </form></table></body></html>
2、index_ok.php
<?php$title=$_post[title];$content=$_post[content];$str="****";$titles = preg_replace("/(黑客)|(抓包)|(监听)/",$str,$title);$contents = preg_replace("/(黑客)|(抓包)|(监听)/",$str,$content);?><html><head><meta http-equiv="content-type" content="text/html; charset=gb2312" /><title>过滤留言板中的非法字符</title><style type="text/css"><!--body { margin-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px;}.style1 { font-size: 12px; color: #855201;}--></style></head><body><table width="1002" height="585" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="400" height="226"> </td> <td width="406"> </td> <td width="196"> </td> </tr> <form id="form1" name="form1" method="post" action="index_ok.php"> <tr> <td height="260"> </td> <td align="left" valign="top"><p class="style1">发布主题:<?php echo $titles;?></p> <p class="style1">发布内容:<?php echo $contents;?></p></td> <td> </td> </tr> <tr> <td> </td> <td align="center" valign="top"> </td> <td> </td> </tr> </form></table></body></html>
二、运行结果
发布主题:****
发布内容:****客 ****包
相关推荐:
php实现的超长文本分页显示功能
以上就是php使用正则表达式实现过滤非法字符串功能示例的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product