首页 > 后端开发 > php教程 > 求教,php写正则匹配html代码的数据

求教,php写正则匹配html代码的数据

WBOY
发布: 2016-06-23 14:10:46
原创
846 人浏览过

我想要获得 【学生图书】 的名字,图书的href URL地址,图书的图片地址,销售量,售价,市场价,怎么写正则匹配呢,我写的正则老是出问题?匹配不到数据

html代码

<div class="detail"><div><a href="http://a.m.tmall.com/i19614323487.htm?sid=b7a1bfbd834f7a71">学生图书一xxxx</a></div><table border="0" cellspacing="0" cellpadding="0"><tr><td class="pic border" style="width:100px;height:100px;text-align:center;vertical-align:middle;"><a href="http://a.m.tmall.com/i19614323487.htm?sid=b7a1bfbd834f7a71"><img src="http://q.i01.wimg.taobao.com/bao/uploaded/i2/19264023005683587/T1Fs0MXqleXXXXXXXX_!!0-item_pic.jpg_100x100.jpg" alt="学生图书一" /></a></td><td valign="middle"><p>售出:336笔<br/><span class="red">¥139.00</span><br /><del class="gray"   style="max-width:90%">¥239.00</del>                    </p></td></tr></table><!--    139.00  --></div></div><hr/>   <!--catid=50010850, catPath= -->                    <div class="box"><div class="detail"><div><a href="http://a.m.tmall.com/i23681020274.htm?sid=b7a1bfbd834f7a71">学生图书二</a></div><table border="0" cellspacing="0" cellpadding="0"><tr><td class="pic border" style="width:100px;height:100px;text-align:center;vertical-align:middle;"><a href="http://a.m.tmall.com/i23681020274.htm?sid=b7a1bfbd834f7a71"><img src="http://q.i04.wimg.taobao.com/bao/uploaded/i2/19264021080891188/T1abNLXu8fXXXXXXXX_!!0-item_pic.jpg_100x100.jpg" alt="学生图书二" /></a></td><td valign="middle"><p>售出:266笔<br/><span class="red">¥189.03</span><br /><del class="gray"   style="max-width:90%">¥339.00</del>                    </p></td></tr></table><!--    189.03  --></div></div><hr/>   <!--catid=50010850, catPath= -->                    <div class="box"><div class="detail"><div><a href="http://a.m.tmall.com/i17747874821.htm?sid=b7a1bfbd834f7a71">学生图书三</a></div><table border="0" cellspacing="0" cellpadding="0"><tr><td class="pic border" style="width:100px;height:100px;text-align:center;vertical-align:middle;"><a href="http://a.m.tmall.com/i17747874821.htm?sid=b7a1bfbd834f7a71"><img src="http://q.i03.wimg.taobao.com/bao/uploaded/i1/19264021076909972/T1zZBNXr8bXXXXXXXX_!!0-item_pic.jpg_100x100.jpg" alt="三" /></a></td><td valign="middle"><p>售出:120笔<br/><span class="red">¥239.04</span><br /><del class="gray"   style="max-width:90%">¥398.00</del>                    </p></td></tr></table><!--    239.04  --></div></div><hr/>   
登录后复制


回复讨论(解决方案)

preg_match_all('#<div class="box"><div class="detail"><div><a href="([\d\D]*?)">([\d\D]*?)</a></div>#iU',$str,$r);var_dump($r);
登录后复制

preg_match_all('#<div class="box">\s*<div class="detail">\s*<div><a href="(.+)">(.+)</a></div>#iU',$str,$r);print_r($r);
登录后复制

Array
(
    [0] => Array
        (
            [0] => 




            [1] => 



        )

    [1] => Array
        (
            [0] => http://a.m.tmall.com/i23681020274.htm?sid=b7a1bfbd834f7a71
            [1] => http://a.m.tmall.com/i17747874821.htm?sid=b7a1bfbd834f7a71
        )

    [2] => Array
        (
            [0] => 学生图书二
            [1] => 学生图书三
        )

)
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板