php解析HTML

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.