搜尋
首頁後端開發php教程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);
// 找出所有
;哪個屬性id=foo
$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]');
// 查找所有
  • ;在

      $es = $html->find('ul li');
      // 尋找巢狀的
      ;標籤
      $es = $html->find('div div div'); 🎜// Find all in which class=hello
      $es = $html->find('table.hello td');
      // Find all td tags with attribite align=center in table tags
      $es = $html->find(''table td[align=center]');
      // Find all
    • in

        foreach($html->find('ul') as $ul)
        {
        foreach($ul->find('li') as $li)
        {
        // do something...
        }
        }
        // Find first
      • 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教程有興趣的朋友有所幫助。


  • 陳述
    本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
    PHP的當前狀態:查看網絡開發趨勢PHP的當前狀態:查看網絡開發趨勢Apr 13, 2025 am 12:20 AM

    PHP在現代Web開發中仍然重要,尤其在內容管理和電子商務平台。 1)PHP擁有豐富的生態系統和強大框架支持,如Laravel和Symfony。 2)性能優化可通過OPcache和Nginx實現。 3)PHP8.0引入JIT編譯器,提升性能。 4)雲原生應用通過Docker和Kubernetes部署,提高靈活性和可擴展性。

    PHP與其他語言:比較PHP與其他語言:比較Apr 13, 2025 am 12:19 AM

    PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。

    PHP與Python:核心功能PHP與Python:核心功能Apr 13, 2025 am 12:16 AM

    PHP和Python各有優勢,適合不同場景。 1.PHP適用於web開發,提供內置web服務器和豐富函數庫。 2.Python適合數據科學和機器學習,語法簡潔且有強大標準庫。選擇時應根據項目需求決定。

    PHP:網絡開發的關鍵語言PHP:網絡開發的關鍵語言Apr 13, 2025 am 12:08 AM

    PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

    PHP:許多網站的基礎PHP:許多網站的基礎Apr 13, 2025 am 12:07 AM

    PHP成為許多網站首選技術棧的原因包括其易用性、強大社區支持和廣泛應用。 1)易於學習和使用,適合初學者。 2)擁有龐大的開發者社區,資源豐富。 3)廣泛應用於WordPress、Drupal等平台。 4)與Web服務器緊密集成,簡化開發部署。

    超越炒作:評估當今PHP的角色超越炒作:評估當今PHP的角色Apr 12, 2025 am 12:17 AM

    PHP在現代編程中仍然是一個強大且廣泛使用的工具,尤其在web開發領域。 1)PHP易用且與數據庫集成無縫,是許多開發者的首選。 2)它支持動態內容生成和麵向對象編程,適合快速創建和維護網站。 3)PHP的性能可以通過緩存和優化數據庫查詢來提升,其廣泛的社區和豐富生態系統使其在當今技術棧中仍具重要地位。

    PHP中的弱參考是什麼?什麼時候有用?PHP中的弱參考是什麼?什麼時候有用?Apr 12, 2025 am 12:13 AM

    在PHP中,弱引用是通過WeakReference類實現的,不會阻止垃圾回收器回收對象。弱引用適用於緩存系統和事件監聽器等場景,需注意其不能保證對象存活,且垃圾回收可能延遲。

    解釋PHP中的__ Invoke Magic方法。解釋PHP中的__ Invoke Magic方法。Apr 12, 2025 am 12:07 AM

    \_\_invoke方法允許對象像函數一樣被調用。 1.定義\_\_invoke方法使對象可被調用。 2.使用$obj(...)語法時,PHP會執行\_\_invoke方法。 3.適用於日誌記錄和計算器等場景,提高代碼靈活性和可讀性。

    See all articles

    熱AI工具

    Undresser.AI Undress

    Undresser.AI Undress

    人工智慧驅動的應用程序,用於創建逼真的裸體照片

    AI Clothes Remover

    AI Clothes Remover

    用於從照片中去除衣服的線上人工智慧工具。

    Undress AI Tool

    Undress AI Tool

    免費脫衣圖片

    Clothoff.io

    Clothoff.io

    AI脫衣器

    AI Hentai Generator

    AI Hentai Generator

    免費產生 AI 無盡。

    熱門文章

    R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
    3 週前By尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O.最佳圖形設置
    3 週前By尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O.如果您聽不到任何人,如何修復音頻
    3 週前By尊渡假赌尊渡假赌尊渡假赌
    WWE 2K25:如何解鎖Myrise中的所有內容
    4 週前By尊渡假赌尊渡假赌尊渡假赌

    熱工具

    SAP NetWeaver Server Adapter for Eclipse

    SAP NetWeaver Server Adapter for Eclipse

    將Eclipse與SAP NetWeaver應用伺服器整合。

    禪工作室 13.0.1

    禪工作室 13.0.1

    強大的PHP整合開發環境

    SecLists

    SecLists

    SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

    Dreamweaver CS6

    Dreamweaver CS6

    視覺化網頁開發工具

    MantisBT

    MantisBT

    Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。