0

我有一个示例代码:

$content = '
<table width="100%" border="0"> 
   <tr>
      <td style="white-space:nowrap;" class="cap_row_odd">browser_id</td>
      <td width="100%" class="cap_row_odd">browser_winmo_iemobile9</td>
   </tr>
   <tr>
      <td style="white-space:nowrap;" class="cap_row_even  cap_isempty"><span>nokia_feature_pack</span></td>                
      <td width="100%" class="cap_row_even cap_isempty"></td>             
   </tr>
</table>
';

我使用 DOMDocument:

$dom = new DOMDocument();
@$dom->loadHTML($content);
$xpath = new DOMXPath($dom);

$attributes = array();

$query1 = $xpath->query("//td[@style='white-space:nowrap']");
foreach($query1 as $a) { 
    $attributes[] = $a->nodeValue;
}

print_r($attributes);

=> 但是结果为空,如何得到呢?

4

1 回答 1

0

;您从 xpath 查询中遗漏了一个:

$query1 = $xpath->query("//td[@style='white-space:nowrap;']"); // works

考虑使用contains()函数使您的查询更加健壮:

$query1 = $xpath->query("//td[contains(@style, 'white-space:nowrap')]"); // works too
于 2012-07-26T08:51:48.970 回答