sub-info
是类,而不是标记名,因此您使用DOMDocument是有缺陷的,您可能更好地使用XPath查询.
$strhtml=''; $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 . 'iPhone 4S
16GB Black
'; /* 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']; }