標籤:html解析 php
PHP Simple HTML DOM 解析器顯然是相當不多的html檔案解析工具,他可以在伺服器端採用類似於jquery的方式進行dom尋找和修改。目前這個解析器支援PHP5。
但是,這個首先將html進行了標籤解析,佔用了大量的記憶體。在解析複雜一點的html檔案時,甚至佔用超過10M的記憶體,現在在高並發的情況下,這個是無法接受的。
當你載入了5個10個甚至更多的文檔的時候,用完一個就清理記憶體:
<?php$html->clear();?>
該興趣的朋友可以訪問該項目的地址為:http://simplehtmldom.sourceforge.net/ 。
該解析的中文手冊線上地址為:http://www.ecartchina.com/php-simple-html-dom/manual.htm。
您也可以通過上傳到CSDN的離線版:
本文提供同類的div尋找和修改操作方法:
<?phplibxml_use_internal_errors(true) ;$doc = new DOMDocument();// We don't want to bother with white spaces$doc->preserveWhiteSpace = false;$doc->loadHTMLFile("testdoc.html");$xpath = new DOMXPath($doc);//尋找帶aconf_edit_section的div元素$query = '//div[@aconf_edit_section]';//$entries = $xpath->query($query);$entries=$xpath->query($query);foreach ($entries as $entry) { //echo " {$entry->nodeValue}<br/>"; print_r($entry);}// $rs = $dom->getElementById("test");// echo $rs->nodeValue;// print_r($entries->save('xxx.html'));function domNodeList_to_string($DomNodeList) { $output = ''; $doc = new DOMDocument; $i=0; while ( $node = $DomNodeList->item($i) ) { // import node $domNode = $doc->importNode($node, true); // append node $doc->appendChild($domNode); $i++; } $output = $doc->saveXML(); $output = print_r($output, 1); // I added this because xml output and ajax do not like each others //$output = htmlspecialchars($output); return $output;}echo domNodeList_to_string($entries);?>
http://hi.baidu.com/tdweb/item/ac6e00ed831dbc3a87d9de2e
PHP DOMXpath 查詢運算式詳解:
http://blog.csdn.net/wmsjlihuan/article/details/9000790
XPath 文法
http://www.w3school.com.cn/xpath/xpath_syntax.asp