sub-info是类,而不是标记名,因此您对DOMDocument的使用存在缺陷,您可能更好地使用XPath查询.
$strhtml='
iPhone 4S
16GB Black
$doc = new DOMDocument();
$doc->loadHTML( $strhtml );
$xpath=new DOMXPath( $doc );
$col=$xpath->query('//div[@class="sub-info"]/h2|//div[@class="sub-info"]/h3');
if( $col ){
/* You could store results from query in an array */
$tags=array();
foreach( $col as $node ) {
/* Simplest form to display results on separate lines, use br tag */
echo $node->nodeValue . '
';
/* Add tags to array - a rethink would be required if there are multiple h2 and h3 tags! */
$tags[ $node->tagName ]=$node->nodeValue;
}
/* echo back results from array */
echo $tags['h2'];
echo '
';
echo $tags['h3'];
}