• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php解析HTML

php 搞代码 4年前 (2022-01-23) 25次浏览 已收录 0个评论

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();// W本文来源gao@daima#com搞(%代@#码@网&搞gaodaima代码e 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}
"; 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.gaodaima.com/wmsjlihuan/article/details/9000790

XPath 语法

http://www.w3school.com.cn/xpath/xpath_syntax.asp


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php解析HTML

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址