Maison  >  Article  >  développement back-end  >  求教,php写正则匹配html代码的数据

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

WBOY
WBOYoriginal
2016-06-23 14:10:46815parcourir

我想要获得 【学生图书】 的名字,图书的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] => 学生图书三
        )

)
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn