使用preg_match获取图像

时间:2012-10-24 13:28:03

标签: php html image preg-match src

我必须遵守代码:

<div class="beeldengeluid-infobox" style="float: right; margin: 0em 0em 1em 1em; border: 1px #999 solid; background-color: #ffffff;">
<table border="0" cellspacing="0" cellpadding="5" width="300" summary="samenvattend persoonsoverzicht">
      <tr><td colspan="2" style="font-size:8pt;text-align:center;padding:4pt;line-height:1.25em"><div class="center"><div class="thumb tnone"><div class="thumbinner" style="width:252px;"><img alt="" src="http://www.beeldengeluidwiki.nl//images/thumb/c/c8/Claudia_de_Breij_%28c%29_Corbino.jpg/250px-Claudia_de_Breij_%28c%29_Corbino.jpg" width="250" height="333" class="thumbimage" />  <div class="thumbcaption"><div class="magnify"><img src="http://www.beeldengeluidwiki.nl//skins/common/images/magnify-clip.png" width="15" height="11" alt="" /></div>Claudia de Breij<br>Foto: <a rel="nofollow" target="_blank" class="external text" href="http://www.corbino.nl/">Corbino</div></div></div></div></td></tr>
      <tr><th>Naam</th><td>Claudia de Breij</td></tr>
      <tr><th>Geboren</th><td>Utrecht, 13 maart 1975</td></tr>
      <tr><th>Functies</th><td>Presentator, Cabaretier</td></tr>
      <tr><th>Bekend van</th><td><i>VARA laat</i>, <i>De wereld draait door</i>, <i>VARA live</i>, <i>De kunst van het maken</i>, <i>Claudia d'r op</i></td></tr>
      <tr><th>Periode actief</th><td>1995 - heden</td></tr>
      <tr><th>Werkt samen met</th><td>Dolf Jansen, Jan Mulder</td></tr>
      <tr><th>Trivia</th><td>Is getrouwd met presentatrice Conny Kraaijeveld</td></tr>
      <tr><th>Media</th><td><div><map name="ImageMap_1_902718077"><area href="http://www.beeldengeluidwiki.nl/index.php/Audio:_Claudia_de_Breij" class="plainlinks" rel="nofollow" shape="rect" coords="0,4,26,25" alt="Audio fragmenten" title="Audio fragmenten"/></map><img alt="Audio.png" src="http://www.beeldengeluidwiki.nl//images/7/72/Audio.png" width="25" height="23" usemap="#ImageMap_1_902718077"/></div></td></tr>
      <tr><th>Externe info</th><td><a rel="nofollow" target="_blank" class="external text" href="http://www.claudiadebreij.nl/">officiële site</td></tr>
      <tr><td colspan="2"><br/>Claudia de Breij in de media<br/>Oeuvre van Claudia de Breij</td></tr>
      </table>
    </div>
   <p>Claudia de Breij is cabaretière, zangeres en radio- en televisiepresentatrice.     
   </p>
   <hr />

现在我想获取可变的图片网址。为此,我有这个代码:

  <div class="thumbinner" style="width:252px;"><img alt="" src="/images/thumb/0/03/HettyBlok.jpg/250px-HettyBlok.jpg" width="250" height="278"           class="thumbimage" /></div>

但我不知道怎么做到这一点。

如何获取图片网址?

我试过了:

preg_match('@<img.+src="(.*)".*>@Uims', $cm->rev, $matches);
$src = $matches[0];
echo $src;  

这适合我,但现在我想用div来获取,因为源中有多个图像,我想要正确的图像。

我试过这个:

preg_match('/<div class = "thumbinne" style="width:252px;"><img.+src="(.*)".*><\/div>/s',$cm->rev,$matches);
$src = $matches[0];
echo $src;      

但这不起作用。

1 个答案:

答案 0 :(得分:3)

您可以使用DOM Parse:

$dom = new DOMDocument;
$dom->loadHTML('<div class="thumbinner" style="width:252px;"><img alt="" src="/images/thumb/0/03/HettyBlok.jpg/250px-HettyBlok.jpg" width="250" height="278" class="thumbimage" /></div>');
$x = new DOMXPath($dom); 

foreach($x->query("//img") as $node) 
{
    echo $node->getAttribute("src");
}

工作样本:http://codepad.org/szqI92Z8

另一种方式:

$dom = new DOMDocument;
$dom->loadHTML('<div class="beeldengeluid-infobox" style="float: right; margin: 0em 0em 1em 1em; border: 1px #999 solid; background-color: #ffffff;">
<table border="0" cellspacing="0" cellpadding="5" width="300" summary="samenvattend persoonsoverzicht">
      <tr><td colspan="2" style="font-size:8pt;text-align:center;padding:4pt;line-height:1.25em"><div class="center"><div class="thumb tnone"><div class="thumbinner" style="width:252px;"><img alt="" src="http://www.beeldengeluidwiki.nl//images/thumb/c/c8/Claudia_de_Breij_%28c%29_Corbino.jpg/250px-Claudia_de_Breij_%28c%29_Corbino.jpg" width="250" height="333" class="thumbimage" />  <div class="thumbcaption"><div class="magnify"><img src="http://www.beeldengeluidwiki.nl//skins/common/images/magnify-clip.png" width="15" height="11" alt="" /></div>Claudia de Breij<br>Foto: <a rel="nofollow" target="_blank" class="external text" href="http://www.corbino.nl/">Corbino</div></div></div></div></td></tr>
      <tr><th>Naam</th><td>Claudia de Breij</td></tr>
      <tr><th>Geboren</th><td>Utrecht, 13 maart 1975</td></tr>
      <tr><th>Functies</th><td>Presentator, Cabaretier</td></tr>
      <tr><th>Bekend van</th><td><i>VARA laat</i>, <i>De wereld draait door</i>, <i>VARA live</i>, <i>De kunst van het maken</i>, <i>Claudia d\'r op</i></td></tr>
      <tr><th>Periode actief</th><td>1995 - heden</td></tr>
      <tr><th>Werkt samen met</th><td>Dolf Jansen, Jan Mulder</td></tr>
      <tr><th>Trivia</th><td>Is getrouwd met presentatrice Conny Kraaijeveld</td></tr>
      <tr><th>Media</th><td><div><map name="ImageMap_1_902718077"><area href="http://www.beeldengeluidwiki.nl/index.php/Audio:_Claudia_de_Breij" class="plainlinks" rel="nofollow" shape="rect" coords="0,4,26,25" alt="Audio fragmenten" title="Audio fragmenten"/></map><img alt="Audio.png" src="http://www.beeldengeluidwiki.nl//images/7/72/Audio.png" width="25" height="23" usemap="#ImageMap_1_902718077"/></div></td></tr>
      <tr><th>Externe info</th><td><a rel="nofollow" target="_blank" class="external text" href="http://www.claudiadebreij.nl/">officiële site</td></tr>
      <tr><td colspan="2"><br/>Claudia de Breij in de media<br/>Oeuvre van Claudia de Breij</td></tr>
      </table>
    </div>
   <p>Claudia de Breij is cabaretière, zangeres en radio- en televisiepresentatrice.     
   </p>
   <hr />');
$x = new DOMXPath($dom); 

$images = array();

foreach($x->query("//div[contains(@class,'thumbinner')]/img") as $node) 
{
  $images[]=$node->getAttribute("src");
}

echo "<pre>";
print_r ($images);