首頁 > 後端開發 > php教程 > 求教,php写正则匹配html代码的数据

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

WBOY
發布: 2016-06-23 14:10:46
原創
844 人瀏覽過

我想要获得 【学生图书】 的名字,图书的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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板