PHP使用正则表达式实现过滤非法字符串功能示例
网络编程 2021-07-05 08:24www.168986.cn编程入门
这篇文章主要介绍了PHP使用正则表达式实现过滤非法字符串功能,结合留言板数据提交功能示例分析了php使用正则表达式preg_replace函数进行字符串正则替换相关操作技巧,需要的朋友可以参考下
本文实例讲述了PHP使用正则表达式实现过滤非法字符串功能。分享给大家供大家参考,具体如下:
一、代码
1、index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://.w3./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://.w3./1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>过滤留言板中的非法字符</title> <style type="text/css"> <!-- body { margin-left: 0px; margin-: 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=""><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=""><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-: 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=""><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=""> </td> <td> </td> </tr> </form> </table> </body> </html>
二、运行结果
发布主题:****
发布内容:****客 ****包
PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:
JavaScript正则表达式在线测试工具:
正则表达式在线生成工具:
更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》及《》
希望本文所述对大家PHP程序设计有所帮助。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程