首頁 後端開發 php教程 tooyoungtoosimple simplehtmldom Doc api幫助文檔

tooyoungtoosimple simplehtmldom Doc api幫助文檔

Jul 29, 2016 am 08:48 AM

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 <td> in <table> 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 <li> in <ul>
      foreach($html->find('ul') as $ul)
      {
      foreach($ul->find('li') as $li)
      {
      // do something...
      }
      }
      // Find first <li> in first <ul>
      $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("<div>foo <b>bar</b></div>");
      $e = $html->find("div", 0);
      echo $e->tag; // Returns: " div"
      echo $e->outertext; // Returns: " <div>foo <b>bar</b></div>"
      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

    熱門文章

    兩個點博物館:邦格荒地地點指南
    4 週前 By 尊渡假赌尊渡假赌尊渡假赌
    倉庫:如何復興隊友
    3 週前 By 尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island冒險:如何獲得巨型種子
    3 週前 By 尊渡假赌尊渡假赌尊渡假赌

    熱門文章

    兩個點博物館:邦格荒地地點指南
    4 週前 By 尊渡假赌尊渡假赌尊渡假赌
    倉庫:如何復興隊友
    3 週前 By 尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island冒險:如何獲得巨型種子
    3 週前 By 尊渡假赌尊渡假赌尊渡假赌

    熱門文章標籤

    記事本++7.3.1

    記事本++7.3.1

    好用且免費的程式碼編輯器

    SublimeText3漢化版

    SublimeText3漢化版

    中文版,非常好用

    禪工作室 13.0.1

    禪工作室 13.0.1

    強大的PHP整合開發環境

    Dreamweaver CS6

    Dreamweaver CS6

    視覺化網頁開發工具

    SublimeText3 Mac版

    SublimeText3 Mac版

    神級程式碼編輯軟體(SublimeText3)

    11個最佳PHP URL縮短腳本(免費和高級) 11個最佳PHP URL縮短腳本(免費和高級) Mar 03, 2025 am 10:49 AM

    11個最佳PHP URL縮短腳本(免費和高級)

    在Laravel中使用Flash會話數據 在Laravel中使用Flash會話數據 Mar 12, 2025 pm 05:08 PM

    在Laravel中使用Flash會話數據

    6個額外的技能,每個PHP開發人員都應該擁有 6個額外的技能,每個PHP開發人員都應該擁有 Feb 28, 2025 am 10:52 AM

    6個額外的技能,每個PHP開發人員都應該擁有

    了解PHP中的陣列 了解PHP中的陣列 Feb 28, 2025 am 10:53 AM

    了解PHP中的陣列

    簡化的HTTP響應在Laravel測試中模擬了 簡化的HTTP響應在Laravel測試中模擬了 Mar 12, 2025 pm 05:09 PM

    簡化的HTTP響應在Laravel測試中模擬了

    構建具有Laravel後端的React應用程序:第2部分,React 構建具有Laravel後端的React應用程序:第2部分,React Mar 04, 2025 am 09:33 AM

    構建具有Laravel後端的React應用程序:第2部分,React

    在Codecanyon上的12個最佳PHP聊天腳本 在Codecanyon上的12個最佳PHP聊天腳本 Mar 13, 2025 pm 12:08 PM

    在Codecanyon上的12個最佳PHP聊天腳本

    php中的捲曲:如何在REST API中使用PHP捲曲擴展 php中的捲曲:如何在REST API中使用PHP捲曲擴展 Mar 14, 2025 am 11:42 AM

    php中的捲曲:如何在REST API中使用PHP捲曲擴展

    See all articles