php html解析器Simple HTML Dom使用说明
本文章来给大家介绍一下关于Simple HTML Dom解析器的使用方法详解,有需要了解的同学不防进入参考。
1. 开始使用
首先下载解压缩,然后将simple_html_dom.php文件包含进要编写的脚本文件中,加载要处理的html,支持三种模式的html加载,分别是『从url中加载,从字符串中加载,从文件中加载』。
代码如下 | 复制代码 |
require_once('simple_html_dom.php'); //从文件中加载 $html = file_get_html('example.htm'); 从字符串加载网上文件需要先从网络下下载,使用cURL比较好一些,需要在php配置文件中打开php扩展php_curl。 $url = 'http://www.111cn.net'; |
2. 查找html元素
使用find函数查找,返回包含对象的数组,常见的查找如下。
代码如下 | 复制代码 |
//查找超链接元素 $alink = $html->find('a'); //查找第n个连接元素 $alink = $html->find('a',5); //查找id为main的div $mainDiv = $html->find('div[id=main]'); //查找所有定义了id的div $idDiv = $html->find('div[id]'); //查找所有定义了id的元素 $idAll = $html->find('[id]'); //查找样式类为info的元素 $classInfo = $html->find('.info'); //支持嵌套子元素查找 $ret = $html->find('ul li'); //查找多个html元素 $ret = $html->find('a,img,p'); //.... |
3. 其他
可以使用内置的函数来进行元素的定位,返回父元素parent,返回子元素数组children,返回第一个子元素first_child,返回最后一个子元素last_child,返回前一个相邻元素prev_sibling,返回后一个相邻元素next_sibling等。
提供简单的正则表达式来过滤属性选择器,类似于[attribute]的格式。
每个对象都有4个基本属性:
tag — 返回html标签名
innertext — 返回innerHTML
outertext — 返回outerHTML
plaintext — 返回HTML标签中的文本
返回元素属性值
//返回$alink的href值
$link = $alink->href;
通过设置元素的属性值可以对元素进行添加、修改、删除操作。
代码如下 | 复制代码 |
//删除url连接 $ret->outertext = ''; $ret->outertext = $ret->outertext . ' other ';$ret->outertext = ' Welcome ' . $ret->outertext; -EOF- |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
