正则表达式学习

一. 匹配规则的单引号 双引号 区别

1         public function match_test(){
2             $pattern = '/<span.*class="tt">.*p(\d{2})p.*<\/span>/im';
3             $str = "<div class='dd'><span class='tt'>p3443pfakj;p33p</span></div>";
4             preg_match($pattern, $str , $match);
5             
6             print_r($match);
7         }


上面的 $pattern 里面 使用的 是 '/……/' 时  会发现 匹配不成功  但是 改成 "/……/" 后 匹配成功。具体原因 暂时不清楚,稍后会补上。

 

二. 模式修正符  s  (如果设定了此修正符,模式中的圆点元字符(.)匹配所有的字符,包括换行符。没有此设定的话,则不包括换行符)

        public function match_test(){
            $pattern = "/<span\s*class='tt'>.*p(\d{2})p.*<\/span>/is";
            $str = "<div class='dd'>
            <span class='tt'>
            p3443pfakj;p33p
            </span>
            </div>";
            preg_match($pattern, $str , $match);
            
            print_r($match);
        }

 上面如果没有使用到 s 则匹配肯定不成功  s 将目标字符串 视为一行来进行 匹配 所以能够匹配成功
 元字符 \s 表示匹配 空白字符

 

三. 防止贪婪匹配 使用模式修正符 U

 1         public function match_test(){
 2             $pattern = "/\s*<span\s*class='tt'>\s*<a.*href='(.*)'>.*p\d{2}p.*<\/a>\s*<\/span>\s*/is";
 3             $str = "<div class='dd'>
 4             <span class='tt'>
 5             <a href='http://localhost/11.html'>justp11p</a>
 6             </span>
 7             
 8             <span class='tt'>
 9             <a href='http://localhost/22.html'>justp22p</a>
10             </span>
11             
12             <span class='tt'>
13             <a href='http://localhost/33.html'>justp33p</a>
14             </span>
15             
16             <span class='tt'>
17             <a href='http://localhost/44.html'>justp44p</a>
18             </span>
19             </div>";
20             preg_match_all($pattern, $str , $match);
21             
22             print_r($match);
23         }

没有使用 模式修正符 U 时 匹配的结果为

1 Array ( [0] => Array ( [0] => justp11p justp22p justp33p justp44p ) [1] => Array ( [0] => http://localhost/44.html ) ) 

使用后 U 后 匹配结果为:

1 Array ( [0] => Array ( [0] => justp11p [1] => justp22p [2] => justp33p [3] => justp44p ) [1] => Array ( [0] => http://localhost/11.html [1] => http://localhost/22.html [2] => http://localhost/33.html [3] => http://localhost/44.html ) ) 

 

 

最后:

 

View Code
 1     public function match_test(){
 2             $pattern = "/\s*<span\s*class='tt'>\s*<a.*href='http\:\/\/localhost\/(\d{2})\.html'>.*p\d{2}p.*<\/a>\s*<\/span>\s*/isU";
 3             $str = "<div class='dd'>
 4             <span class='tt'>
 5             <a href='http://localhost/11.html'>justp11p</a>
 6             </span>
 7             
 8             <span class='tt'>
 9             <a href='http://localhost/22.html'>justp22p</a>
10             </span>
11             
12             <span class='tt'>
13             <a href='http://localhost/33.html'>justp33p</a>
14             </span>
15             
16             <span class='tt'>
17             <a href='http://localhost/44.html'>justp44p</a>
18             </span>
19             </div>";
20             preg_match_all($pattern, $str , $match);
21             
22             print_r($match);
23         }

 

 

四. 现在用了接近2个小时的时间 发现了一个问题 就是 使用 file_get_content() 函数 抓取淘宝页面时 匹配一直失败

 

 1         public function getListByCid(){
 2             ob_start();
 3             $cid = intval($_GET['cid']);
 4             //$url = 'http://'.'shop'.$this->shopId.'.'.'taobao.com/?search=y&scid='.$cid;
 5             $url = "http://shop33277279.taobao.com/?search=y&scid=459681844";
 6             $content = file_get_contents($url);
 7         //    $result = $this->match($content);
 8             $content = mb_convert_encoding($content, "utf-8", "gb2312");
 9             $pattern = "/<div\s*class='item'>\s*<div\s*class='pic'>\s*<a\s*href='.*id=(.*)&'.*>.*<\/a>/isU";
10             preg_match_all($pattern, $content , $match);
11                 
12             print_r($match);
13         }


发现使用 上面的规则 一直匹配失败 匹配规则 都是正确的  唯一有问题 的就是 单引号的问题

只要 将 $pattern 里面的 '//' 改成 下图所示

1 $pattern = '/<div\s*class="item">\s*<div\s*class="pic">\s*<a\s*href=".*id=(.*)&".*>.*<\/a>/isU';

就可以成功匹配。

问题原因在于:

  => 使用 file_get_content() 采集到的远程 淘宝的页面内容 全部是 <div class="item">……</div> 这种形式  所以 必须使用 <div\s*class="item">这种形式

来记性匹配 。

 

 

 

 

 

 

 

 

 

posted @ 2013-03-28 14:30  justphp  阅读(184)  评论(0)    收藏  举报