Home php教程 PHP源码 php html解析器Simple HTML Dom使用说明

php html解析器Simple HTML Dom使用说明

Jun 08, 2016 pm 05:23 PM
curl find gt html

本文章来给大家介绍一下关于Simple HTML Dom解析器的使用方法详解,有需要了解的同学不防进入参考。

<script>ec(2);</script>

 

1. 开始使用

首先下载解压缩,然后将simple_html_dom.php文件包含进要编写的脚本文件中,加载要处理的html,支持三种模式的html加载,分别是『从url中加载,从字符串中加载,从文件中加载』。

 代码如下 复制代码

require_once('simple_html_dom.php');
//从url加载
$html = file_get_html('http://www.111cn.net');
//从字符串加载
$html = str_get_html('

Hello World!');
//从文件中加载
$html = file_get_html('example.htm');
从字符串加载网上文件需要先从网络下下载,使用cURL比较好一些,需要在php配置文件中打开php扩展php_curl。

$url = 'http://www.111cn.net';
$ci = curl_init();
curl_setopt($ci,CURLOPT_URL,$url);
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);

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连接
$alink->href = null;
//元素的修改
$ret->outertext = '

';
$ret->outertext = '';
$ret->outertext = $ret->outertext . '
other
';
$ret->outertext = '
Welcome
' . $ret->outertext;
-EOF-
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

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

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

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.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

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

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

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

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

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

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

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

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

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

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

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

See all articles