使用PHP DomDocument提取文本和图像src

时间:2014-08-22 15:18:06

标签: php text extract domdocument

我试图在div id =" Ajax"中提取img src和TD的文本。但是我无法用我的代码提取img。它只是忽略了img src。我如何提取img src并将其添加到数组中?

HTML:

<div id="Ajax">
<table cellpadding="1" cellspacing="0">
<tbody>
<tr id="comment_1">
<td>20:28</td>
<td class="color">
</td>
<td class="last_comment">
Text<br/>
</td>
</tr>
<tr id="comment_2">
<td>20:25</td>
<td class="color">
</td>
<td class="comment">
Text 2<br/>
</td>
</tr>
<tr id="comment_3">
<td>20:24</td>
<td class="color">
<img src="http://url.ext/img/image02.jpeg" alt="img alt 2"/>
</td>
<td class="comment">
Text 3<br/>
</td>
</tr>
<tr id="comment_4">
<td>20:23</td>
<td class="color">
<img src="http://url.ext/img/image01.jpeg" alt="img alt"/>
</td>
<td class="comment">
Text 4<br/>
</td>
</tr>
</div>

PHP:

$html = file_get_contents($url);

$doc = new DOMDocument();
@$doc->loadHTML($html);
$contentArray = array();
$doc = $doc->getElementById('Ajax');
$text = $doc->getElementsByTagName ('td');
foreach ($text as $t)
{
$contentArray[] = $t->nodeValue;
}
print_r ($contentArray);

感谢。

1 个答案:

答案 0 :(得分:0)

您正在使用$t->nodeValue来获取节点的内容。 <img>标记为空,因此无法返回。获取src属性的最简单方法是XPath。

示例:

$html = file_get_contents($url);

$doc = new DOMDocument();
@$doc->loadHTML($html);
$xpath = new DOMXpath($doc);
$expression = "//div[@id='Ajax']//tr"; 
$nodes = $xpath->query($expression); // Get all rows (tr) in the div

$imgSrcExpression = ".//img/@src";
$firstTdExpression = "./td[1]";
foreach($nodes as $node){ // loop over each row
  // select the first td node
  $tdNodes = $xpath->query($firstTdExpression ,$node);
  $tdVal = null;
  if($tdNodes->length > 0){
    $tdVal = $tdNodes->item(0)->nodeValue;
  }

  // select the src attribute of the img node
  $imgNodes = $xpath->query($imgSrcExpression,$node);
  $imgVal = null;
  if($imgNodes ->length > 0){
    $imgVal = $imgNodes->item(0)->nodeValue;
  }
}

(注意:代码可能包含拼写错误)