从表preg_match_all中提取div部分

时间:2014-01-31 13:12:09

标签: php preg-match-all

嗨我想通过php preg_match_all从html中提取div部分,我有如下所示的多个表,想要从<中提取所有div部分td>标签,div就像那样开始

$regex ='\<td width=\"178\" height=\"44\" valign=\"top\" background=\"img\/comprar-boton.jpg\" style=\"background-repeat:no-repeat\">(.*?)<\/div>/i'; 
preg_match_all($regex,$pageHtml,$out);
print_r($out);

 <table border="0" align="center" cellpadding="1" cellspacing="1">
                    <tr>
                      <td align="center"><a  href="inicio.php?bq=2&amp;id=2005" title="">
                      <div style="margin-right:10px;">
                      <img src="productos/producto_4779.jpg" width="200" border="0" class="BtnRecorrido" />
                      </div>
                      </a></td>
                      </tr>
                    <tr>
                      <td align="center" class="MonsterTitulos"><strong>BOXER CALAVERAS</strong></td>
                      </tr>
                    <tr>
                      <td width="178" height="44" valign="top" background="img/comprar-boton.jpg" style="background-repeat:no-repeat"><div > 
                      <a href="inicio.php?bq=2&amp;id=2005">
                        <div class="textoHueso12" align="center">$110.00 MXN</div>
                        </a></div>
                        </td>
                      </tr>
                    </table>

1 个答案:

答案 0 :(得分:0)

使用DOMDocument Class。

<?php
$content=' <table border="0" align="center" cellpadding="1" cellspacing="1">
                    <tr>
                      <td align="center"><a  href="inicio.php?bq=2&amp;id=2005" title="">
                      <div style="margin-right:10px;">
                      <img src="productos/producto_4779.jpg" width="200" border="0" class="BtnRecorrido" />
                      </div>
                      </a></td>
                      </tr>
                    <tr>
                      <td align="center" class="MonsterTitulos"><strong>BOXER CALAVERAS</strong></td>
                      </tr>
                    <tr>
                      <td width="178" height="44" valign="top" background="img/comprar-boton.jpg" style="background-repeat:no-repeat"><div >
                      <a href="inicio.php?bq=2&amp;id=2005">
                        <div class="textoHueso12" align="center">$110.00 MXN</div>
                        </a></div>
                        </td>
                      </tr>
                    </table>';
$dom = new DOMDocument;
$dom->loadHTML($content);
foreach ($dom->getElementsByTagName('td') as $tag) {
    foreach($tag->getElementsByTagName('div') as $divtag)
    {
        foreach($divtag->getElementsByTagName('img') as $imgtag)
        {
         echo $imgtag->getAttribute('src');
        }
    }
}

<强> OUTPUT :

productos/producto_4779.jpg