在留言板中,有时需要对用户输入内容进行过滤,将一些非法与特殊字符串进行过滤处理,将其替换为*。下面本篇文章就来给大家分享一下过滤功能的实现代码,希望对大家有所帮助!
需求:用户在评论页面输入非法字符以后,需要将非法字符替换为*
简单实现方法:
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>
运行结果
复杂实现方法:可过滤JS 、PHP标签
//简单过滤JS 、PHP标签 function cleanJs($html){ $html=trim($html); $html=str_replace(array('<?','?>'),array('<?','?>'),$html); $pattern=array( "'<script[^>]*?>.*?</script>'si", "'<style[^>]*?>.*?</style>'si", "'<frame[^>]*?>'si", "'<iframe[^>]*?>.*?</iframe>'si", "'<link[^>]*?>'si" ); $replace=array("","","","",""); return preg_replace($pattern,$replace,$html); } /* Remove JS/CSS/IFRAME/FRAME 过滤JS/CSS/IFRAME/FRAME/XSS等恶意攻击代码(可安全使用) * Return string */ function cleanJsCss($html){ $html=trim($html); $html=preg_replace('/\0+/', '', $html); $html=preg_replace('/(\\\\0)+/', '', $html); $html=preg_replace('#(&\#*\w+)[\x00-\x20]+;#u',"\\1;",$html); $html=preg_replace('#(&\#x*)([0-9A-F]+);*#iu',"\\1\\2;",$html); $html=preg_replace("/%u0([a-z0-9]{3})/i", "&#x\\1;", $html); $html=preg_replace("/%([a-z0-9]{2})/i", "&#x\\1;", $html); $html=str_replace(array('<?','?>'),array('<?','?>'),$html); $html=preg_replace('#\t+#',' ',$html); $scripts=array('javascript','vbscript','script','applet','alert','document','write','cookie','window'); foreach($scripts as $script){ $temp_str=""; for($i=0;$i<strlen($script);$i++){ $temp_str.=substr($script,$i,1)."\s*"; } $temp_str=substr($temp_str,0,-3); $html=preg_replace('#'.$temp_str.'#s',$script,$html); $html=preg_replace('#'.ucfirst($temp_str).'#s',ucfirst($script),$html); } $html=preg_replace("#<a.+?href=.*?(alert\(|alert&\#40;|javascript\:|window\.|document\.|\.cookie|<script|<xss).*?\>.*?</a>#si", "", $html); $html=preg_replace("#<img .+?src=.*?(alert\(|alert&\#40;|javascript\:|window\.|document\.|\.cookie|<script|<xss).*?\ alt="How to filter illegal and special strings in php" >#si", "", $html); $html=preg_replace("#<(script|xss).*?\>#si", "<\\1>", $html); $html=preg_replace('#(<[^>]*?)(onblur|onchange|onclick|onfocus|onload|onmouseover|onmouseup|onmousedown|onselect|onsubmit|onunload|onkeypress|onkeydown|onkeyup|onresize)[^>]*>#is',"\\1>",$html); //$html=preg_replace('#<(/*\s*)(alert|applet|basefont|base|behavior|bgsound|blink|body|embed|expression|form|frameset|frame|head|html|ilayer|iframe|input|layer|link|meta|object|plaintext|style|script|textarea|title|xml|xss)([^>]*)>#is', "<\\1\\2\\3>", $html); $html=preg_replace('#<(/*\s*)(alert|applet|basefont|base|behavior|bgsound|blink|body|expression|form|frameset|frame|head|html|ilayer|iframe|input|layer|link|meta|object|plaintext|style|script|textarea|title|xml|xss)([^>]*)>#is', "<\\1\\2\\3>", $html); $html=preg_replace('#(alert|cmd|passthru|eval|exec|system|fopen|fsockopen|file|file_get_contents|readfile|unlink)(\s*)\((.*?)\)#si', "\\1\\2(\\3)", $html); $bad=array( 'document.cookie' => '', 'document.write' => '', 'window.location' => '', "javascript\s*:" => '', "Redirect\s+302" => '', '<!--' => '<!--', '-->' => '-->' ); foreach ($bad as $key=>$val){ $html=preg_replace("#".$key."#i",$val,$html); } return $html; } //过滤html标签以及敏感字符 function cleanHtml($html){ return cleanYellow(htmlspecialchars($html)); } //过滤部分HTML标签 function cleanFilter($html){ $html=trim($html); $html=preg_replace("/<p[^>]*?>/is","<p>",$html); $html=preg_replace("/<div[^>]*?>/is","<div>",$html); $html=preg_replace("/<ul[^>]*?>/is","<ul>",$html); $html=preg_replace("/<li[^>]*?>/is","<li>",$html); $html=preg_replace("/<span[^>]*?/is","<span>",$html); $html=preg_replace("/<a[^>]*?>(.*)?<\/a>/is","\${1}",$html); $html=preg_replace("/<table[^>]*?>/is","<table>",$html); $html=preg_replace("/<tr[^>]*?>/is","<tr>",$html); $html=preg_replace("/<td[^>]*?>/is","<td>",$html); $html=preg_replace("/<ol[^>]*?>/is","<ol>",$html); $html=preg_replace("/<form[^>]*?>/is","",$html); $html=preg_replace("/<input[^>]*?>/is","",$html); return $html; } //过滤非法的敏感字符串 function cleanYellow($txt){ $txt=str_replace( array("黄色","性爱","做爱","我日","我草","我靠","尻","共产党","胡锦涛","毛泽东", "政府","中央","研究生考试","性生活","色情","情色","我考","麻痹","妈的","阴道", "淫","奸","阴部","爱液","阴液","臀","色诱","煞笔","傻比","阴茎","法轮功","性交","阴毛","江泽民"), array("*1*","*2*","*3*","*4*","*5*","*6*","*7*","*8*","*9*","*10*", "*11*","*12*","*13*","*14*","*15*","*16*","*17*","*18*","*19*","*20*", "*21*","*22*","*23*","*24*","*25*","*26*","*27*","*28*","*29*","*30*","*31*","*32*","*33*","*34*"), $txt); return $txt; } //过滤敏感字符串以及恶意代码 function cleanAll($html){ return cleanYellow(cleanJsCss($html)); } //全半角字符替换 function setFilter($html){ $arr=array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9', 'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E', 'F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J', 'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O', 'P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T', 'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y', 'Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd', 'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i', 'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n', 'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's', 't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x', 'y' => 'y', 'z' => 'z', '(' => '(', ')' => ')', '〔' => '[', '〕' => ']', '【' => '[', '】' => ']', '〖' => '[', '〗' => ']', '“' => '[', '”' => ']', '‘' => '[', '’' => ']', '{' => '{', '}' => '}', '《' => '<', '》' => '>', '%' => '%', '+' => '+', '—' => '-', '-' => '-', '~' => '-', ':' => ':', '。' => '.', '、' => ',', ',' => '.', '、' => '.', ';' => ',', '?' => '?', '!' => '!', '…' => '-', '‖' => '|', '”' => '"', '’' => '`', '‘' => '`', '|' => '|', '〃' => '"', ' ' => ' '); return strtr($html,$arr); }
推荐学习:《PHP视频教程》

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools