首頁 php教程 php手册 simplehtmldom Doc api帮助文档

simplehtmldom Doc api帮助文档

Jun 13, 2016 pm 12:01 PM
api doc helper o reference 幫助 文件

API Reference

Helper functions
object str_get_html ( string $content ) Creates a DOM object from a string.
object file_get_html ( string $filename ) Creates a DOM object from a file or a URL.

DOM methods & properties

stringplaintext Returns the contents extracted from HTML.
voidclear () Clean up memory.
voidload ( string $content ) Load contents from a string.
stringsave ( [string $filename] ) Dumps the internal DOM tree back into a string. If the $filename is set, result string will save to file.
voidload_file ( string $filename ) Load contents from a from a file or a URL.
voidset_callback ( string $function_name ) Set a callback function.
mixedfind ( string $selector [, int $index] ) Find elements by the CSS selector. Returns the Nth element object if index is set, otherwise return an array of object.

Element methods & properties

string[attribute] Read or write element's attribure value.
stringtag Read or write the tag name of element.
stringoutertext Read or write the outer HTML text of element.
stringinnertext Read or write the inner HTML text of element.
stringplaintext Read or write the plain text of element.
mixedfind ( string $selector [, int $index] ) Find children by the CSS selector. Returns the Nth element object if index is set, otherwise, return an array of object.

DOM traversing

mixed$e->children ( [int $index] ) Returns the Nth child object if index is set, otherwise return an array of children.
element$e->parent () Returns the parent of element.
element$e->first_child () Returns the first child of element, or null if not found.
element$e->last_child () Returns the last child of element, or null if not found.
element$e->next_sibling () Returns the next sibling of element, or null if not found.
element$e->prev_sibling () Returns the previous sibling of element, or null if not found.
Camel naming convertions You can also call methods with W3C STANDARD camel naming convertions.


string$e->getAttribute ( $name ) string$e->attribute
void$e->setAttribute ( $name, $value ) void$value = $e->attribute
bool$e->hasAttribute ( $name ) boolisset($e->attribute)
void$e->removeAttribute ( $name ) void$e->attribute = null
element$e->getElementById ( $id ) mixed$e->find ( "#$id", 0 )
mixed$e->getElementsById ( $id [,$index] ) mixed$e->find ( "#$id" [, int $index] )
element$e->getElementByTagName ($name ) mixed$e->find ( $name, 0 )
mixed$e->getElementsByTagName ( $name [, $index] ) mixed$e->find ( $name [, int $index] )
element$e->parentNode () element$e->parent ()
mixed$e->childNodes ( [$index] ) mixed$e->children ( [int $index] )
element$e->firstChild () element$e->first_child ()
element$e->lastChild () element$e->last_child ()
element$e->nextSibling () element$e->next_sibling ()
element$e->previousSibling () element$e->prev_sibling ()





// Create a DOM object from a string
$html = str_get_html('

Hello!');

// Create a DOM object from a URL
$html = file_get_html('http://www.google.com/');

// Create a DOM object from a HTML file
$html = file_get_html('test.htm');



// Create a DOM object
$html = new simple_html_dom();

// Load HTML from a string
$html->load('Hello!');

// Load HTML from a URL
$html->load_file('http://www.google.com/');

// Load HTML from a HTML file
$html->load_file('test.htm');


// Find all anchors, returns a array of element objects
$ret = $html->find('a');

// Find (N)thanchor, returns element object or null if not found(zero based)
$ret = $html->find('a', 0);

// Find all
which attribute id=foo
$ret = $html->find('div[id=foo]');

// Find all
with the id attribute
$ret = $html->find('div[id]');

// Find all element has attribute id
$ret = $html->find('[id]');


// Find all element which id=foo
$ret = $html->find('#foo');

// Find all element which class=foo
$ret = $html->find('.foo');

// Find all anchors and images
$ret = $html->find('a, img');

// Find all anchors and images with the "title" attribute
$ret = $html->find('a[title], img[title]');



// Find all
  • in

      $es = $html->find('ul li');

      // Find Nested
      tags
      $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; // Returns: " foo bar"
          echo $e->plaintext; // Returns: " foo bar"


          $e->tag Read or write the tag name of element.
          $e->outertext Read or write the outer HTML text of element.
          $e->innertext Read or write the inner HTML text of element.
          $e->plaintext Read or write the plain text of element.

          // Extract contents from HTML
          echo $html->plaintext;

          // Wrap a element
          $e->outertext = '
          ' . $e->outertext . '
          ';

          // Remove a element, set it's outertext as an empty string
          $e->outertext = '';

          // Append a element
          $e->outertext = $e->outertext . '
          foo
          ';

          // Insert a element
          $e->outertext = '
          foo
          ' . $e->outertext;

          // If you are not so familiar with HTML DOM, check this link to learn more...

          // Example
          echo $html->find("#div1", 0)->children(1)->children(1)->children(2)->id;
          // or
          echo $html->getElementById("div1")->childNodes(1)->childNodes(1)->childNodes(2)->getAttribute('id');
          You can also call methods with Camel naming convertions.

          mixed$e->children ( [int $index] ) Returns the Nth child object if index is set, otherwise return an array of children.
          element$e->parent () Returns the parent of element.
          element$e->first_child () Returns the first child of element, or null if not found.
          element$e->last_child () Returns the last child of element, or null if not found.
          element$e->next_sibling () Returns the next sibling of element, or null if not found.
          element$e->prev_sibling () Returns the previous sibling of element, or null if not found.

          // Dumps the internal DOM tree back into string
          $str = $html;

          // Print it!
          echo $html;

          // Dumps the internal DOM tree back into string
          $str = $html->save();

          // Dumps the internal DOM tree back into a file
          $html->save('result.htm');

          // Write a function with parameter "$element"
          function my_callback($element) {
          // Hide all tags
          if ($element->tag=='b')
          $element->outertext = '';
          }

          // Register the callback function with it's function name
          $html->set_callback('my_callback');

          // Callback function will be invoked while dumping
          echo $html;
  • 本網站聲明
    本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

    熱AI工具

    Undresser.AI Undress

    Undresser.AI Undress

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

    AI Clothes Remover

    AI Clothes Remover

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

    Undress AI Tool

    Undress AI Tool

    免費脫衣圖片

    Clothoff.io

    Clothoff.io

    AI脫衣器

    Video Face Swap

    Video Face Swap

    使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

    熱工具

    記事本++7.3.1

    記事本++7.3.1

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

    SublimeText3漢化版

    SublimeText3漢化版

    中文版,非常好用

    禪工作室 13.0.1

    禪工作室 13.0.1

    強大的PHP整合開發環境

    Dreamweaver CS6

    Dreamweaver CS6

    視覺化網頁開發工具

    SublimeText3 Mac版

    SublimeText3 Mac版

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

    熱門話題

    Java教學
    1655
    14
    CakePHP 教程
    1413
    52
    Laravel 教程
    1306
    25
    PHP教程
    1252
    29
    C# 教程
    1226
    24
    如何在 IDE 中查看 Golang 函數文件? 如何在 IDE 中查看 Golang 函數文件? Apr 18, 2024 pm 03:06 PM

    使用IDE檢視Go函數文件:將遊標停留在函數名稱上。按下熱鍵(GoLand:Ctrl+Q;VSCode:安裝GoExtensionPack後,F1並選擇"Go:ShowDocumentation")。

    PHP中的Web Service PHP中的Web Service Mar 27, 2024 am 08:06 AM

    隨著網路和行動裝置的普及,WebService(網路服務)已成為一個不可或缺的技術。現在,WebService已經成為一個標準化的通訊協議,因此它可以被各種系統完成。而在PHP開發中,WebService也是一個非常重要且常見的技術。本文將探討PHP中的Web服務,包括基礎知識與使用方法。 1.什麼是Web服務? Web服務的概念,是指在Web上提供

    全球數字貨幣交易十大APP推薦(2025貨幣交易軟件排名) 全球數字貨幣交易十大APP推薦(2025貨幣交易軟件排名) Mar 12, 2025 pm 05:48 PM

    本文推薦全球十大數字貨幣交易APP,涵蓋幣安(Binance)、OKX、火幣(Huobi Global)、Coinbase、Kraken、Gate.io、KuCoin、Bitfinex、Gemini和Bitstamp。這些平台在交易對數量、交易速度、安全性、合規性、用戶體驗等方面各有特色,例如幣安以其高交易速度和廣泛服務聞名,而Coinbase則更適合新手用戶。選擇適合自己的平台需要綜合考慮自身需求和風險承受能力。 了解全球主流數字貨幣交易平台,助您安全高效進行數字資產交易。

    btc交易app怎麼安裝註冊? btc交易app怎麼安裝註冊? Feb 21, 2025 pm 07:09 PM

    本篇文章將詳細介紹如何安裝和註冊比特幣交易應用。比特幣交易應用允許用戶管理和交易比特幣等加密貨幣。文章逐步指導用戶完成安裝和註冊過程,包括下載應用程序、創建賬戶、進行身份驗證和首次存款。文章的目標是為初學者提供清晰易懂的指南,幫助他們輕鬆進入比特幣交易的世界。

    golang框架文件使用說明 golang框架文件使用說明 Jun 05, 2024 pm 06:04 PM

    如何使用Go框架文檔?確定文件類型:官網、GitHub儲存庫、第三方資源。了解文件結構:入門指南、深入教學、參考手冊。根據需要定位資訊:使用組織結構或搜尋功能。理解術語和概念:仔細閱讀並理解新的術語和概念。實戰案例:使用Beego創建一個簡單的Web伺服器。其他Go框架文件:Gin、Echo、Buffalo、Fiber。

    歐易交易所下載官方入口 歐易交易所下載官方入口 Feb 21, 2025 pm 07:51 PM

    歐易,又稱OKX,是一個全球領先的加密貨幣交易平台。文章提供了歐易官方安裝包的下載入口,方便用戶在不同設備上安裝歐易客戶端。該安裝包支持 Windows、Mac、Android 和 iOS 系統,用戶可根據自己的設備類型選擇相應版本下載。安裝完成後,用戶即可註冊或登錄歐易賬戶,開始交易加密貨幣和享受平台提供的其他服務。

    在不同的 PHP 框架中,哪一個比較適合用來建立高效能 API? 在不同的 PHP 框架中,哪一個比較適合用來建立高效能 API? Jun 01, 2024 pm 07:56 PM

    對於高效能PHPAPI開發,最佳框架的選擇取決於目標API類型、預期負載和可用資源。 Laravel擁有快速路由系統、強大快取機制和內建事件系統,而Symfony則具有模組化架構、高效能HTTP基準測試和內建事件調度器。 ZendFramework提供高速路由、整合的快取框架和即用的基準測試工具。

    如何為 Golang 函數文件撰寫清晰簡潔的描述? 如何為 Golang 函數文件撰寫清晰簡潔的描述? May 01, 2024 pm 03:15 PM

    若要為Go函數編寫清晰的文檔,請遵循慣例並使用godoc註釋語法。為函數名稱、參數和返回值添加註釋,使用Markdown標記增強文檔,並使用清晰的語言闡明函數的目的和用途。提供具體細節,使用帶有註釋的程式碼範例展示函數的行為,並涵蓋錯誤處理。

    See all articles