[英]PHP DOMDocument how to get element?
我試圖閱讀一個網站的內容,但我有一個問題,我想得到圖像,鏈接這些元素,但我想得到他們自己的元素,而不是元素內容,例如我想得到它:我想得到整個元素。
我怎樣才能做到這一點..
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.link.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$dom = new DOMDocument;
@$dom->loadHTML($output);
$items = $dom->getElementsByTagName('a');
for($i = 0; $i < $items->length; $i++) {
echo $items->item($i)->nodeValue . "<br />";
}
curl_close($ch);;
?>
您似乎要求獲取DOMElement的序列化html ? 例如,您想要一個包含<a href="http://example.org">link text</a>
的字符串嗎? (請讓你的問題更清楚。)
$url = 'http://example.com';
$dom = new DOMDocument();
$dom->loadHTMLFile($url);
$anchors = $dom->getElementsByTagName('a');
foreach ($anchors as $a) {
// Best solution, but only works with PHP >= 5.3.6
$htmlstring = $dom->saveHTML($a);
// Otherwise you need to serialize to XML and then fix the self-closing elements
$htmlstring = saveHTMLFragment($a);
echo $htmlstring, "\n";
}
function saveHTMLFragment(DOMElement $e) {
$selfclosingelements = array('></area>', '></base>', '></basefont>',
'></br>', '></col>', '></frame>', '></hr>', '></img>', '></input>',
'></isindex>', '></link>', '></meta>', '></param>', '></source>',
);
// This is not 100% reliable because it may output namespace declarations.
// But otherwise it is extra-paranoid to work down to at least PHP 5.1
$html = $e->ownerDocument->saveXML($e, LIBXML_NOEMPTYTAG);
// in case any empty elements are expanded, collapse them again:
$html = str_ireplace($selfclosingelements, '>', $html);
return $html;
}
但請注意,您正在做的事情很危險,因為它可能會混合編碼。 最好將輸出作為另一個DOMDocument,並使用importNode()
復制所需的節點。 或者,使用XSL樣式表。
我假設你只是復制粘貼了一些示例代碼,並沒有費心去學習它是如何工作的......
無論如何, ->nodeValue
部分獲取元素並返回文本內容(因為該元素有一個單獨的文本節點子節點 - 如果它還有其他內容,我不知道nodeValue
會給出什么)。
所以,只需刪除->nodeValue
獲得元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.