请教一下正则过滤

#1 gw2012

$str=preg_replace("/\s+/", " ", $str); //过滤多余回车 
$str=preg_replace("/<[ ]+/si","<",$str); //过滤<__("<"号后面带空格)

$str=preg_replace("/<\!--.*?-->/si","",$str); //注释
$str=preg_replace("/<(\!.*?)>/si","",$str); //过滤DOCTYPE
$str=preg_replace("/<(\/?html.*?)>/si","",$str); //过滤html标签
$str=preg_replace("/<(\/?head.*?)>/si","",$str); //过滤head标签
$str=preg_replace("/<(\/?meta.*?)>/si","",$str); //过滤meta标签
$str=preg_replace("/<(\/?body.*?)>/si","",$str); //过滤body标签
$str=preg_replace("/<(\/?link.*?)>/si","",$str); //过滤link标签
$str=preg_replace("/<(\/?form.*?)>/si","",$str); //过滤form标签
$str=preg_replace("/cookie/si","COOKIE",$str); //过滤COOKIE标签

$str=preg_replace("/<(applet.*?)>(.*?)<(\/applet.*?)>/si","",$str); //过滤applet标签
$str=preg_replace("/<(\/?applet.*?)>/si","",$str); //过滤applet标签

$str=preg_replace("/<(style.*?)>(.*?)<(\/style.*?)>/si","",$str); //过滤style标签
$str=preg_replace("/<(\/?style.*?)>/si","",$str); //过滤style标签

$str=preg_replace("/<(title.*?)>(.*?)<(\/title.*?)>/si","",$str); //过滤title标签
$str=preg_replace("/<(\/?title.*?)>/si","",$str); //过滤title标签

$str=preg_replace("/<(object.*?)>(.*?)<(\/object.*?)>/si","",$str); //过滤object标签
$str=preg_replace("/<(\/?objec.*?)>/si","",$str); //过滤object标签

$str=preg_replace("/<(noframes.*?)>(.*?)<(\/noframes.*?)>/si","",$str); //过滤noframes标签
$str=preg_replace("/<(\/?noframes.*?)>/si","",$str); //过滤noframes标签

$str=preg_replace("/<(i?frame.*?)>(.*?)<(\/i?frame.*?)>/si","",$str); //过滤frame标签
$str=preg_replace("/<(\/?i?frame.*?)>/si","",$str); //过滤frame标签

$str=preg_replace("/<(script.*?)>(.*?)<(\/script.*?)>/si","",$str); //过滤script标签
$str=preg_replace("/<(\/?script.*?)>/si","",$str); //过滤script标签
$str=preg_replace("/javascript/si","Javascript",$str); //过滤script标签
$str=preg_replace("/vbscript/si","Vbscript",$str); //过滤script标签
$str=preg_replace("/on([a-z]+)\s*=/si","On\\1=",$str); //过滤script标签
$str=preg_replace("/&/si","&#",$str); //过滤script标签,如javAsCript:alert(
以上是过滤规则
我想把权限留言本提交上来的title,content其内容经过上面的规则过滤,然后再进入verifier进行验证
请问如何编写



这是model
class lib_guestbook extends spModel
{
        var $pk = "gid"; // 每个留言唯一的标志,可以称为主键
        var $table = "topic"; // 数据表的名称
               
        var $verifier = array( // 留言内容验证规则
                "rules" => array(
                        'title' => array(
                                'notnull' => TRUE,
                                'minlength' => 3,
                                'maxlength' => 45
                        ),
                        'contents' => array(
                                'notnull' => TRUE,
                                'minlength' => 10,
                                'maxlength' => 1000
                        ),
                ),
                "messages" => array(
                        'title' => array(
                                'notnull' => "标题不能为空",
                                'minlength' => "标题必须大于3个字符",
                                'maxlength' => "标题必须小于45个字符"
                        ),
                        'contents' => array(
                                'notnull' => "内容不能为空",
                                'minlength' => "内容必须大于10个字符",
                                'maxlength' => "内容必须小于1000个字符",
                        ),
                )
        );
       
       
        // 请注意,这里我们覆盖了spModel的create函数,以方便我们对新增的记录加入时间与用户名
        public function create($row){
                // 使用array_merge构造新的$row
                $row = array_merge($row, array(
                        'ctime' => time(),
                        'uname' => $_SESSION["userinfo"]["uname"]
                ));
                // 调用父类(spModel)的create方法
                parent::create($row);
        }
}
就是在开始验证前把那些标签过滤掉


这是controller
public function post(){
                if( $title = $this->spArgs("title") ){
               
                        // 已经提交,开始对数据进行验证
                        $guestbookObj = spClass("lib_guestbook"); // 实例化留言对象
                       
                        // 这里直接验证全部的提交数据($this->spArgs()获取全部提交数据)
                        $results = $guestbookObj->spVerifier($this->spArgs());
                        if( false == $results ){ // 当spVerifier返回false的时候,则是表示已经通过验证,数据是合格的
                       
                                // 调用lib_guestbook类的create来新增记录
                                // 请注意!这里的create并非原来spModel的create,
                                // 这个create函数是通过覆盖的方式,在lib_guestbook中已经重写了,以便加入新记录时间和用户名两个数据
                                // 请查看lib_guestbook.php文件的create方法
                                $guestbookObj->create($this->spArgs());
                                $this->success("创建新话题成功!若页面没有您的话题,请刷新!","javascript:window.history.go(-2);");
                        }else{
                                // $results不是false,所以没有通过验证,错误信息是$results
                                // dump($results);
                                foreach($results as $item){
                                        // 每一个规则,都有可能返回多个错误信息,所以这里我们也循环$item来获取多个信息
                                        foreach($item as $msg){
                                                // 虽然我们使用了循环,但是这里我们只需要第一条出错信息就行。
                                                // 所以取到了第一条错误信息的时候,我们使用$this->error来提示并跳转
                                                $this->error($msg,"javascript:window.history.go(-1);");
                                        }
                                }
                        }
                }
                $this->results = spClass("lib_forum")->findAll(NULL,"id ASC","id,name");
                // 这里是还没提交,则自动显示main_post.html的模板               
        }
请问规则应该写在哪个位置

2012-06-30 17:28:23

#2

你要过滤什么[tthread=IvyStk, 谢扬]http://app.qlogo.cn/mbloghead/cf811e192006a0f7f966[/tthread]

2012-06-30 19:45:07

#3

||@IvyStk: 你要过滤什么[tthread=gw5070, Useforum]http://app.qlogo.cn/mbloghead/4ddd1319489e9172f77a[/tthread]

2012-06-30 19:45:08

#4

正则表达式是个很神奇的东西,可以算是一个极其强大的匹配器了。 || @IvyStk:你要过滤什么[tthread=gengdada, 达达]http://app.qlogo.cn/mbloghead/4f2a3e78fe7d3176f8ba[/tthread]

2012-06-30 19:45:09

#5 jake

自定义验证规则,手册 http://www.speedphp.com/spverifier-create-rules.html

2012-06-30 19:45:19