検索
ホームページphp教程php手册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 セレクターで子を検索します。インデックスが設定されている場合は N 番目の要素オブジェクトを返し、それ以外の場合はオブジェクトの配列を返します。

DOM traversing

mixed$e->children ( [int $index] ) インデックスが設定されている場合は N 番目の子オブジェクトを返し、それ以外の場合は子の配列を返します。
element$e->parent () 要素の親を返します。
element$e->first_child () 要素の最初の子を返します。見つからない場合は null を返します。
element$e->last_child () 要素の最後の子を返します。見つからない場合は null を返します。
element$e->next_sibling () 要素の次の兄弟を返します。見つからない場合は null を返します。
element$e->prev_sibling () 要素の前の兄弟を返します。見つからない場合は null を返します。
Camel 命名変換 W3C STANDARD Camel 命名変換を使用してメソッドを呼び出すこともできます。


string$e->getAttribute ( $name ) string$e->attribute
void$e->setAttribute ( $name, $value ) void$value = $e ->属性
bool$e->hasAttribute ( $name ) boolisset($e->attribute)
void$e->removeAttribute ( $name ) void$e->attribute = null
element$e->getElementById ( $id ) 混合$e->find ( "#$id", 0 )
mixed$e->getElementsById ( $id [,$index] ) 混合$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 ()
要素$e->previousSibling () element$e->




// 文字列から 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) アンカーを検索し、要素オブジェクトを返すか、見つからない場合は 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');

      // 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; // 戻り値: " foo bargt;"
          echo $e->plaintext; // 戻り値: " foo bar"


          $e->tag 要素のタグ名の読み取りまたは書き込み
          $e->outertext 要素の外側の HTML テキストの読み取りまたは書き込み。要素の内部 HTML テキストの読み取りまたは書き込み。

          // HTML からコンテンツを抽出します。
          echo $html->plaintext ;

          // 要素をラップします
          $e->outertext = '
          ' . $e->outertext . '
          ';

          // 要素を削除し、空の文字列として設定します
          $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 $html ->getElementById("div1")->childNodes(1)->childNodes(1)->getAttribute('id');
          Camel を使用してメソッドを呼び出すこともできます。

          mixed$e->children ( [int $index] ) インデックスが設定されている場合は N 番目の子オブジェクトを返し、それ以外の場合は子の配列を返します。 () 要素の親を返します。
          element$e->first_child () 要素の最初の子を返します。見つからない場合は null を返します。
          element$e->last_child () の最後の子を返します。
          element$e->next_sibling () 要素の次の兄弟を返します。
          element$e->prev_sibling () 前の兄弟を返します。

          // 内部 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;
  • 声明
    この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

    ホットAIツール

    Undresser.AI Undress

    Undresser.AI Undress

    リアルなヌード写真を作成する AI 搭載アプリ

    AI Clothes Remover

    AI Clothes Remover

    写真から衣服を削除するオンライン AI ツール。

    Undress AI Tool

    Undress AI Tool

    脱衣画像を無料で

    Clothoff.io

    Clothoff.io

    AI衣類リムーバー

    Video Face Swap

    Video Face Swap

    完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

    ホットツール

    SublimeText3 Mac版

    SublimeText3 Mac版

    神レベルのコード編集ソフト(SublimeText3)

    ゼンドスタジオ 13.0.1

    ゼンドスタジオ 13.0.1

    強力な PHP 統合開発環境

    PhpStorm Mac バージョン

    PhpStorm Mac バージョン

    最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

    SecLists

    SecLists

    SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。

    SublimeText3 英語版

    SublimeText3 英語版

    推奨: Win バージョン、コードプロンプトをサポート!