Home > Article > Backend Development > felayman-php_自定义函数过滤指定字符
felayman------php_自定义函数过滤指定字符
<pre code_snippet_id="279207" snippet_file_name="blog_20140407_1_2389317" name="code" class="php"><?php /** * Created by PhpStorm. * User: FelayMan * Date: 14-4-7 * Time: 下午5:09 */ function filter_str($str){ $arr = array('图书','明日科技','软件','变成词典','词典'); //implode将数组连接成字符串 $repstr = implode(',',$arr); echo $repstr; //preg_match将会在$repstr中检索$str,如果出现则会停止检索 if(preg_match("/$str/",$repstr)){ echo "<script> alert('您使用了禁用词语'); window.location.href='filter_string.php5'; "; }else{ echo $str; } } function filter_words($str){ //假设我无法在留言板中输入中国 $words = "中国"; if(preg_match("/$words/",$str)){ echo "<script> alert('你的留言者包含了危险词语'); window.location.href='filter_string.php5'; </script>"; }else{ echo "$str"; } } if(!empty($_POST['sub'])){ // filter_str($_POST['content']); filter_words($_POST['content']); }?> <title>自定义函数过滤器</title> <meta charset="utf-8">