ホームページ  >  記事  >  バックエンド開発  >  simplehtmldom Doc API ヘルプ document_PHP チュートリアル

simplehtmldom Doc API ヘルプ document_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-21 15:19:44648ブラウズ

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 トラバース

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->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 $インデックス] )
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 ()





// 文字列から 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)thanchor を検索し、要素オブジェクトを返すか、見つからない場合は 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 bar"
          echo $e->plaintext; // 戻り値: " foo bar"


          $e- >tag 要素のタグ名を読み取りまたは書き込みます。
          $e->outertext 要素の外側の HTML テキストを読み取りまたは書き込みます。
          $e->innertext 要素の内部 HTML テキストを読み取りまたは書き込みます。 >plaintext 要素のプレーンテキストを読み取るか書き込みます

          // 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)->childNodes(2)->getAttribute('id');
          Camel 名前変換を使用してメソッドを呼び出すこともできます

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



          http://www.bkjia.com/PHPjc/325206.html

          www.bkjia.com

          tru​​e

          http://www.bkjia.com/PHPjc/325206.html

          技術記事

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