php domdocument getelementbyid,PHP DOMDocument- getElementByID添加Â代替空
PHP DOMDocument-> getElementByID添加?代替空(PHP DOMDocument->getElementByID adding ? in place of empty )
我正在使用PHP的DOMDocument對象來解析一些HTML(使用cURL獲取)。 當我按ID獲取元素并輸出它時,任何空的 標記都會獲得一個額外的字符并變為? 。
代碼:
$document = new DOMDocument();
$document->validateOnParse = true;
$document->loadHTML( curl_exec($handle) );
curl_close($handle);
$element = $document->getElementById( __ELEMENT_ID__ );
echo $document->saveHTML();
echo $document->saveHTML($element);
?>
$document->saveHTML()命令按預期運行并打印出整個頁面。 但是,就像我上面說的那樣,在echo $document->saveHTML($element)命令echo $document->saveHTML($element)空標簽轉換為? 。
這發生在$element所有 標記中。
在這個過程中(通過ID獲取元素并輸出元素)是插入這個額外的字符? 我可以解決它,但我更感興趣的是找到根。
I'm using PHP's DOMDocument object to parse some HTML (fetched with cURL). When I get an element by ID and output it, any empty tags get an additional character and become ? .
The Code:
$document = new DOMDocument();
$document->validateOnParse = true;
$document->loadHTML( curl_exec($handle) );
curl_close($handle);
$element = $document->getElementById( __ELEMENT_ID__ );
echo $document->saveHTML();
echo $document->saveHTML($element);
?>
The $document->saveHTML() command behaves as expected and prints out the entire page. BUT, like I say above, on the echo $document->saveHTML($element) command transforms empty tags into ? .
This happens to all tags within $element.
What in this process (of getting the element by ID and outputting the element) is inserting this extra character? I'm could work around it, but I'm more interested in getting to the root.
原文:https://stackoverflow.com/questions/13629351
更新時間:2019-11-29 11:57
最滿意答案
我能夠通過設置頁面的字符編碼來解決問題。 我提取的頁面沒有定義的字符編碼,我的頁面只是一個沒有定義標題信息的片段。 當我添加
問題消失了。
I was able to fix the problem by setting the character encoding of the page. The page I was fetching did not have a defined character encoding, and my page was just a snippet without defined header info. When I added
The problem disappeared.
2012-11-30
相關問答
我能夠通過設置頁面的字符編碼來解決問題。 我提取的頁面沒有定義的字符編碼,我的頁面只是一個沒有定義標題信息的片段。 當我添加
問題消失了。 I was able to fix the problem by setting the character encoding of the page. The page I was
...
你可以使用DOMDocumentFragment和它的appendXML()方法,例如 <?php
$doc = new DOMDocument();
$doc->formatOutput = true;
$ele = $doc->createElement("someele", "Hello");
$xmlstuff = $doc->createElement("otherxmlstuff");
$fragment = $doc->createDocumentFragm
...
function getInnerHtml( $node ) {
$innerHTML= '';
$children = $node->childNodes;
foreach ($children as $child) {
$innerHTML .= $child->ownerDocument->saveXML( $child );
}
return $innerHTML;
}
$html = getInnerHtml($d
...
替換&nbsp; 與&amp; nbsp; 然后當讀取htmlDom文檔時,它將返回&nbsp; replace ? with then when the htmlDom doc is read it will return ?
您可以使用抑制解析錯誤的輸出 libxml_use_internal_errors(true);
要檢查返回的響應是否為404,您可以在調用DOMDocument::load()之后檢查$http_response_header 例: libxml_use_internal_errors(true);
$rssDom = new DOMDocument();
$rssDom->load($url);
if (strpos($http_response_header[0], '404')) {
...
我認為,如果禁用外部實體加載器,則顯然無法加載外部實體 。 唯一的解決方案是使用libxml_disable_entity_loader(false)啟用外部實體的加載。 由于此設置不是線程安全的,我可以看到兩種方法: 全局啟用它并使用其他功能來阻止加載不需要的實體(通常來自網絡): 使用libxml_set_external_entity_loader注冊您自己的實體加載器。 我認為這是最安全的解決方案。 使用解析選項LIBXML_NONET 。 如果您只想禁用libxml2的網絡訪問,這應該足
...
知道了,不知道它是如何無效的 - 證明文件: $xpath = new \DOMXpath($document);
$nodes = $xpath->query('//img[@id="banner"]');
// Return content if we don't have exactly one image with id="banner"
if(1 !== $nodes->length) return $content;
// DOMNode of the banner
$banner
...
用這個: $str = file_get_contents('http://dream-portal.net/index.php/board,65.0.html');
$doc = new DOMDocument();
@$doc->loadHTML($str);
$selector = new DOMXPath($doc);
foreach ($selector->query('//*[starts-with(@id, "msg_")]') as $node) {
var_dump
...
嘗試創建文件的用戶不是“yurow”(可能有權創建該文件的用戶)。 相反,它是一個用戶,如“apache”或“httpd”。 通常,系統設置為禁止apache / httpd用戶在Web根目錄中創建文件。 這是出于安全目的而做的,我不建議通過給webroot提供apache / httpd寫訪問來繞過它。 相反,您可以在/ home / yurow內部創建文檔(不在/ home / yurow / wwwroot內)。 一個例子可能是:/home/yurow/xmldata/test02.xml。
...
嘗試以正確的格式編寫HTML,使用雙引號分隔的屬性值,而不是單引號,因此它們不會被編碼。 Javascript識別由單引號分隔的字符串。 這是一個例子: $html = 'click here';
$doc = new DOMDocument();
$doc->loadHTML( $html );
echo $html . "\n";
echo "-----------------\n";
e
...
總結
以上是生活随笔為你收集整理的php domdocument getelementbyid,PHP DOMDocument- getElementByID添加Â代替空的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 济南必买的特色礼品
- 下一篇: 魔兽世界工会名字195个