首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

Simple HTML DOM Parser API 完整參考手冊(中文版)

     編輯:PHP 綜合知識

簡介

Simple HTML DOM Parser 是一個 PHP 5+ 類庫,提供類似 jQuery 的選擇器語法,輕鬆解析、操作 HTML 文件。支援從字串、檔案、URL 建立 DOM 物件,並提供豐富的遍歷、修改、輸出方法。

核心輔助函式

函式說明
str_get_html(string $content)從字串建立 DOM 物件
file_get_html(string $filename)從檔案或 URL 建立 DOM 物件

DOM 物件方法與屬性

方法/屬性說明
plaintext取得純文字內容(移除所有標籤)
clear()釋放記憶體
load(string $content)從字串載入內容
save([string $filename])輸出內部 DOM 樹為字串,若給檔名則存檔
load_file(string $filename)從檔案/URL 載入
set_callback(string $function_name)設定輸出時的回呼函式
find(string $selector [, int $index])CSS 選擇器查找元素,指定 index 回傳單一物件,否則回傳陣列

元素物件方法與屬性

屬性/方法說明
$e->attribute讀寫屬性值(如 $e->href)
$e->tag讀寫標籤名稱
$e->outertext讀寫外層 HTML(含標籤本身)
$e->innertext讀寫內層 HTML(不含標籤本身)
$e->plaintext讀寫純文字內容
$e->find(string $selector [, int $index])查找子元素,用法同 DOM 物件

DOM 遍歷方法

方法說明
$e->children([int $index])取得子元素陣列或指定索引
$e->parent()取得父元素
$e->first_child()取得第一個子元素
$e->last_child()取得最後一個子元素
$e->next_sibling()取得下一個同層元素
$e->prev_sibling()取得上一個同層元素

W3C DOM 相容別名(駝峰命名)

庫同時支援標準 DOM 方法名,如 getAttribute()、setAttribute()、getElementById()、getElementsByTagName()、parentNode()、childNodes()、firstChild() 等,內部映射至上述短名稱方法。

選擇器語法速查

  • tag:標籤名,如 div
  • #id:ID 選擇器
  • .class:類別選擇器
  • [attr]:擁有該屬性
  • [attr=value]:屬性等於值
  • [attr!=value]:屬性不等於值
  • [attr^=value]:屬性以值開頭
  • [attr$=value]:屬性以值結尾
  • [attr*=value]:屬性包含值
  • a, img:群組選擇器
  • ul li:後代選擇器
  • div > p:子代選擇器(部分版本支援)

常用範例集錦

建立 DOM 物件

$html = str_get_html('<html><body>Hello!</body></html>');
$html = file_get_html('https://www.example.com/');
$html = file_get_html('test.htm');

查找元素

$anchors = $html->find('a');           // 所有連結
$first_a = $html->find('a', 0);        // 第一個連結
$divs = $html->find('div[id=foo]');    // id=foo 的 div
$with_id = $html->find('[id]');        // 所有有 id 的元素
$by_id = $html->find('#foo');          // id=foo
$by_class = $html->find('.foo');       // class=foo
$links_imgs = $html->find('a, img');   // 連結與圖片
$titled = $html->find('a[title], img[title]');

巢狀查找

$lis = $html->find('ul li');
$nested = $html->find('div div div');
$tds = $html->find('table.hello td');
$center_tds = $html->find('table td[align=center]');

屬性操作

$href = $e->href;           // 取得 href
$e->href = 'my link';       // 設定 href
$e->href = null;            // 移除 href
if (isset($e->href)) echo 'href exist!';

內容輸出與修改

echo $html->plaintext;              // 全文純文字
$e->outertext = '<div class="wrap">' . $e->outertext . '</div>'; // 包裹
$e->outertext = '';                 // 移除元素
$e->outertext .= '<div>foo</div>'; // 追加
$e->outertext = '<div>foo</div>' . $e->outertext; // 前插

回呼函式範例

function my_callback($element) {
    if ($element->tag == 'b') $element->outertext = ''; // 隱藏所有 <b>
}
$html->set_callback('my_callback');
echo $html; // 輸出時自動套用回呼

儲存結果

$str = $html->save();        // 回傳字串
$html->save('result.htm');   // 存檔

官方網站:simplehtmldom.sourceforge.net

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved