如果TD
不包含圖像,我需要continue;
。如果TD不包含圖像,如何忽略/繼續?
我嘗試這樣做:
if(!$image){continue;}
,但沒有奏效。
<?php
/////////////////////////////////////////////////////////////////////
$html='
<table>
<tr>
<td colspan="2">
<span>green</span>
<img src="green.gif" />
</td>
</tr>
<tr>
<td>
<span>yellow</span>
no image !!
</td>
<td>
<span>red</span>
<img src="red.gif" />
</td>
</tr>
</table>
<table>
<tr>
<td>
<span>black</span>
<img src="black.gif" />
</td>
</tr>
</table>
';
/////////////////////////////////////////////////////////////////////
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DomXPath($dom);
/////////////////////////////////////////////////////////////////////
$query = $xpath->query('.//table/tr/td');
for($x=0,$results=''; $x<$query->length; $x++)
{
$x1=$x+1;
$color = $query->item($x)->getElementsByTagName('span')->item(0)->nodeValue;
$image = $query->item($x)->getELementsByTagName('img');
if(!$image){continue;}
$image = $image->item(0)->getAttribute('src');
$results .= "color $x1 is : $color - and- image $x1 is : $image<br/>";
}
echo $results;
/////////////////////////////////////////////////////////////////////
?>
我該怎麼辦?
非常感謝你prodigitalson,你的答案是我需要.. –