日期:2014-05-16  浏览次数:20486 次

felayman------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';
                </script>";
        }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']);
    }

?>
<!doctype html>
    <html>
    <head>
        <title>自定义函数过滤器</title>
        <meta charset="utf-8"/>
    </head>
    <body>
    <form action="filter_string.php5" method="post">
        请留言:<textarea rows="7" cols="20" name="content"></textarea>
        <input type="submit" value="提交留言" name="sub"/>
    </form>
    </body>
    </html>