background image

$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 = '<div class="nav">' . $ret->outertext . '</div>';
$ret->outertext = '';
$ret->outertext = $ret->outertext . '<div>other</div>';
$ret->outertext = '<div>Welcome</div>' . $ret->outertext;
-EOF-