tooyoungtoosimple simplehtmldom Doc api幫助文檔
API 參考
幫助函數
object str_get_html ( string $content ) 從字串建立 DOM 物件。
object file_get_html ( string $filename ) 從檔案或 URL 建立 DOM 物件。
DOM 方法與屬性
stringplaintext 傳回從 HTML 擷取的內容。
voidclear() 清理記憶體。
voidload ( string $content ) 從字串中載入內容。
stringsave ( [string $filename] ) 將內部 DOM 樹轉儲回字串。如果設定了$filename,結果字串將會儲存到檔案中。
voidload_file ( string $filename ) 從檔案或 URL 載入內容。
voidset_callback ( string $function_name ) 設定回呼函數。
mixedfind ( string $selector [, int $index] ) 透過 CSS 選擇器找出元素。如果設定了索引,則傳回第 N 個元素對象,否則傳回對象數組。
元素方法與屬性
string[attribute] 讀取或寫入元素的屬性值。
stringtag 讀取或寫入元素的標籤名稱。
stringoutertext 讀取或寫入元素的外部 HTML 文字。
stringinnertext 讀取或寫入元素的內部 HTML 文字。
stringplaintext 讀取或寫入元素的純文字。
mixedfind ( string $selector [, int $index] ) 透過 CSS 選擇器找出子項目。如果設定了index,則傳回第N個元素對象,否則傳回一個物件陣列。
DOM 遍歷
mixed$e->children ( [int $index] ) 如果設定了 index,則傳回第 N 個子對象,否則傳回子物件陣列。
element$e->parent () 傳回元素的父元素。
element$e->first_child () 傳回 element 的第一個子元素,如果沒有找到則傳回 null。
element$e->last_child () 傳回 element 的最後一個子元素,如果找不到則傳回 null。
element$e->next_sibling () 傳回 element 的下一個同級元素,如果沒有找到則傳回 null。
element$e->prev_sibling () 傳回 element 的上一個同級元素,如果沒有找到則傳回 null。
Camel 命名轉換您也可以使用 W3C STANDARD Camel 命名轉換來呼叫方法。
string$e->getAttribute ( $name ) string$e->attribute
void$e->setAttribute ( $name, $value ) void$value = $e->attribute
bool$e->attribute ; $name ) boolisset($e->attribute)
void$e->removeAttribute ( $name ) void$e->attribute = null
element$e->getElementById ( $id ) mix$ e->find ( "# $id", 0 )
mixed$e->getElementsById ( $id [,$index] ) mixed$e->find ( "#$id" [, int $index] )
element$e->getElementByTagName ($ name ) 混合$e->find ( $name, 0 )
mixed$e->getElementsByTagName ( $name [, $index] ) 混合$e->find ( $name [, int $index] )
element$e ->parentNode () element$e->parent ()
mixed$e->childNodes ( [$index] ) mixed$e->children ( [int $index] )
element$e->firstChild () element$ld () element$ e->first_child ()
element$e->lastChild () element$e->last_child ()
element$e- >nextSibling () element$e->next_sibling ()
element$e->previousSibling ()
element$e->previousSibling () ) $e->prev_sibling ()
// 從字串建立DOM 物件
$html = str_get_html('
// 從URL 建立DOM物件
$html = file_get_html('http://www.google.com/');
// 從HTML 檔案建立DOM 物件
$html = file_get_html('test.htm');
// 建立DOM 物件
$html = new simple_html_dom();
// 從字串載入HTML
$html->load('Hello!');
// 從URL 載入HTML
$html->load_file('http://www.google.com/');
// 從HTML 檔案載入HTML
$html->load_file('test.htm');
// 找出所有錨點,返回元素物件數組
$ret = $html->find('a');
// 查找(N)thachor,如果沒有找到則返回元素物件或null(從零開始)
$ret = $html- >find('a', 0);
// 找出所有
$ret = $html->find('div[id=foo]');
// 找出所有;帶有id 屬性
$ret = $html->find('div[id]');
// 找出所有具有屬性id 的元素
$ret = $html->find('[id]' );
// 找出所有id=foo 的元素
$ret = $html->find('#foo');
// 找出所有class=foo 的元素
$ret = $html->find('. foo');
// 找出所有錨點和圖像
$ret = $html->find('a, img');
// 找出所有帶有「title」屬性的錨點和圖片
$ret = $html->find('a[title], img[title]');
// 查找所有
- 中
- in
- in first
$e = $html->find('ul', 0)->find('li', 0);
Supports these operators in attribute selectors:
[attribute] Matches elements that have the specified attribute.
[attribute=value] Matches elements that have the specified attribute with a certain value.
[attribute!=value] Matches elements that don't have the specified attribute with a certain value.
[attribute^=value] Matches elements that have the specified attribute and it starts with a certain value.
[attribute$=value] Matches elements that have the specified attribute and it ends with a certain value.
[attribute*=value] Matches elements that have the specified attribute and it contains a certain value.
// Find all text blocks
$es = $html->find('text');
// Find all comment () blocks
$es = $html->find('comment');
// Get a attribute ( If the attribute is non-value attribute (eg. checked, selected...), it will returns true or false)
$value = $e->href;
// Set a attribute(If the attribute is non-value attribute (eg. checked, selected...), set it's value as true or false)
$e->href = 'my link';
// Remove a attribute, set it's value as null!
$e->href = null;
// Determine whether a attribute exist?
if(isset($e->href))
echo 'href exist!';
// Example
$html = str_get_html("foo bar");
$e = $html->find("div", 0);
echo $e->tag; // Returns: " div"
echo $e->outertext; // Returns: "foo bar"
echo $e->innertext; // 回傳:" foo bar"
echo $e->plaintext; // 回傳:" foo bar"
$e->; tag 讀取或寫入元素的標籤名稱。純文字。
// 刪除一個元素,將其設為空字串
$e->outertext = ''; // 追加一個元素
$e- >outertext = $e->outertext . 'foo'; // 插入一個元素
$e->outertext = 'foo' . $e->outertext;
// 如果您不太熟悉HTML DOM,請查看此連結以了解更多信息...
// 範例
echo $html->find("#div1", 0) ->children(1)->children(1)->children(2)->id;
///
echo 或
echo $html->getElementById("div1")->childNodes(1) ->childNodes(1)->childNodes(2)->getAttribute('id');
也可以使用Camel 命名轉換來呼叫方法。 $e->children ( [int $index] )如果設定了索引,則傳回第N 個子對象,否則傳回子物件陣列。 >first_child () 傳回元素的第一個子物件。 ->next_sibling () 傳回元素的下一個兄弟元素,如果找不到,則傳回null。
// 將內部DOM 樹轉儲回字串
$str = $html;回顯$html;
// 將內部DOM 樹轉儲回字串
$str = $html->save();
/ / 將內部DOM 樹轉儲回檔案中
$html->save('result.htm');
// 寫一個參數為「$element」的函式
function my_callback($element) {
// 隱藏所有;標籤
if ($element->tag=='b')
$element->outertext = '';
}
// 用函數名稱註冊回呼函數
$html->set_callback('my_callback' );
// 轉儲時會呼叫回呼函數
echo $html;
以上就介紹了tooyoungtoosimple simplehtmldom Doc api幫助文檔,包括tooyoungtoosimple方面的內容,希望對PHP教程有興趣的朋友有所幫助。
foreach($html->find('ul') as $ul)
{
foreach($ul->find('li') as $li)
{
// do something...
}
}
// Find first本網站聲明本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn熱AI工具
Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片
AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。
Undress AI Tool
免費脫衣圖片
Clothoff.io
AI脫衣器
Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!
熱門文章
Windows 11 KB5054979中的新功能以及如何解決更新問題3 週前 By DDD如何修復KB5055523無法在Windows 11中安裝?2 週前 By DDDInzoi:如何申請學校和大學3 週前 By DDD如何修復KB5055518無法在Windows 10中安裝?2 週前 By DDDRoblox:Dead Rails - 如何召喚和擊敗Nikola Tesla4 週前 By 尊渡假赌尊渡假赌尊渡假赌熱工具
記事本++7.3.1
好用且免費的程式碼編輯器
SublimeText3漢化版
中文版,非常好用
禪工作室 13.0.1
強大的PHP整合開發環境
Dreamweaver CS6
視覺化網頁開發工具
SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)
在PHP API中說明JSON Web令牌(JWT)及其用例。 Apr 05, 2025 am 12:04 AM
JWT是一種基於JSON的開放標準,用於在各方之間安全地傳輸信息,主要用於身份驗證和信息交換。 1.JWT由Header、Payload和Signature三部分組成。 2.JWT的工作原理包括生成JWT、驗證JWT和解析Payload三個步驟。 3.在PHP中使用JWT進行身份驗證時,可以生成和驗證JWT,並在高級用法中包含用戶角色和權限信息。 4.常見錯誤包括簽名驗證失敗、令牌過期和Payload過大,調試技巧包括使用調試工具和日誌記錄。 5.性能優化和最佳實踐包括使用合適的簽名算法、合理設置有效期、
會話如何劫持工作,如何在PHP中減輕它? Apr 06, 2025 am 12:02 AM
會話劫持可以通過以下步驟實現:1.獲取會話ID,2.使用會話ID,3.保持會話活躍。在PHP中防範會話劫持的方法包括:1.使用session_regenerate_id()函數重新生成會話ID,2.通過數據庫存儲會話數據,3.確保所有會話數據通過HTTPS傳輸。
描述紮實的原則及其如何應用於PHP的開發。 Apr 03, 2025 am 12:04 AM
SOLID原則在PHP開發中的應用包括:1.單一職責原則(SRP):每個類只負責一個功能。 2.開閉原則(OCP):通過擴展而非修改實現變化。 3.里氏替換原則(LSP):子類可替換基類而不影響程序正確性。 4.接口隔離原則(ISP):使用細粒度接口避免依賴不使用的方法。 5.依賴倒置原則(DIP):高低層次模塊都依賴於抽象,通過依賴注入實現。
在PHPStorm中如何進行CLI模式的調試? Apr 01, 2025 pm 02:57 PM
在PHPStorm中如何進行CLI模式的調試?在使用PHPStorm進行開發時,有時我們需要在命令行界面(CLI)模式下調試PHP�...
PHP 8.1中的枚舉(枚舉)是什麼? Apr 03, 2025 am 12:05 AM
PHP8.1中的枚舉功能通過定義命名常量增強了代碼的清晰度和類型安全性。 1)枚舉可以是整數、字符串或對象,提高了代碼可讀性和類型安全性。 2)枚舉基於類,支持面向對象特性,如遍歷和反射。 3)枚舉可用於比較和賦值,確保類型安全。 4)枚舉支持添加方法,實現複雜邏輯。 5)嚴格類型檢查和錯誤處理可避免常見錯誤。 6)枚舉減少魔法值,提升可維護性,但需注意性能優化。
如何在系統重啟後自動設置unixsocket的權限? Mar 31, 2025 pm 11:54 PM
如何在系統重啟後自動設置unixsocket的權限每次系統重啟後,我們都需要執行以下命令來修改unixsocket的權限:sudo...
解釋PHP中的晚期靜態綁定(靜態::)。 Apr 03, 2025 am 12:04 AM
靜態綁定(static::)在PHP中實現晚期靜態綁定(LSB),允許在靜態上下文中引用調用類而非定義類。 1)解析過程在運行時進行,2)在繼承關係中向上查找調用類,3)可能帶來性能開銷。
- in first
$es = $html->find('ul li');
// 尋找巢狀的
$es = $html->find('div div div'); 🎜// Find all