php HTMLのパースの基本的なメモ(Simple HTML DOM Parser)

2011/02/03

PHP Simple HTML DOM Parserのマニュアルを見ても分からない部分があるので、そのメモ。

ライブラリの読込

ライブラリなので読み込む必要あり。

  1. include_once('simple_html_dom.php');

データの読込

使いドコはよくわかんないけど、記載したソースを読む込むときとURLからウェブページを読む込むときで微妙に違う。

HTMLを読み込むとき

  1. $html = str_get_html(“<html><b>aa</b></html>”);

URLからウェブページを読み込むとき

  1. $html = file_get_html(“https://tips.recatnap.info/”);

読み込んだデータから特定の何かを取り出す

上でデータを全部まるっと取り込んでいるのでその中から必要なものを抽出していく。

<body>タグ内にある0番目の<b>タグの中身を抽出

※中身で別のタグが使われていたらそのまま抽出される。

記述パターン1

  1. $gVal = $html->find('body b',0)->innertext;

記述パターン2

  1. $gVaB = $html->find('body b');
  2. $gVal = gVaB[0]->innertext;

<body>タグ内にある1番目の<b>タグを抽出

※中身だけではない。

  1. $gVal = $html->find('body b',1)->outertext;

<body>タグ内にある0番目の<a>タグのhref属性の値を抽出

  1. $gVal = ('body a',0)->href;

<body>タグ内にある0番目の<a>タグのtitle属性の値を抽出

  1. $gVal = ('body a',0)->title;

ソースのサンプル

  1. <?php
  2.  
  3. // PHP Simple HTML DOM Parser
  4. include_once('simple_html_dom.php');
  5.  
  6. $str = “”;
  7. $str .= '<html>' . “\n”;
  8. $str .= '<head>' . “\n”;
  9. $str .= '<title>test Simple HTML DOM Parser</title>' . “\n”;
  10. $str .= '</head>' . “\n”;
  11. $str .= '<body>' . “\n”;
  12. $str .= '<div>' . “\n”;
  13. $str .= 'momomo' . “\n”;
  14. $str .= '<b>hahahahaha</b>' . “\n”;
  15. $str .= 'mamama' . “\n”;
  16. $str .= '<b class=”momo”>hehehe</b>' . “\n”;
  17. $str .= '<b>he<em>he</em>he</b>' . “\n”;
  18. $str .= '<a href=”http://recatnap.info/” title=”linkTitle”>hahahahaha</a>' . “\n”;
  19. $str .= '</div>' . “\n”;
  20. $str .= '</body>' . “\n”;
  21. $str .= '</html>' . “\n”;
  22.  
  23. // HTMLを読み込む
  24. $html = str_get_html($str);
  25.  
  26. // URLからページを読み込むときは下記。
  27. // $html = file_get_html(“https://tips.recatnap.info/”);
  28.  
  29. // <body>の<b>内の0番目のテキスト(innertext)を出力
  30. $b0 = $html->find('body b',0)->innertext;
  31. echo $b0 . “\n”;
  32. // 結果は、hahahahaha
  33.  
  34. // <body>の<b>内の0番目のテキスト(innertext)を出力:記述2
  35. $b3 = $html->find('body b');
  36. echo $b3[0]->innertext . “\n”;
  37. // 結果は、hahahahaha
  38.  
  39. // <body>の<b>内の1番目のタグとテキスト(outertext)を出力
  40. $b1 = $html->find('body b',1)->outertext;
  41. echo $b1 . “\n”;
  42. // 結果は、<b>hehehe</b>
  43.  
  44. // <body>の<b>内の2番目のテキスト(innertext)を出力
  45. $em = $html->find('body b',2)->innertext;
  46. echo $em . “\n”;
  47. // 結果は、he<em>he</em>he
  48.  
  49. // <body>の<a>内の0番目のhref属性の値(href)を出力
  50. $a = $html->find('body a',0)->href;
  51. echo $a . “\n”;
  52. // 結果は、http://recatnap.info/
  53.  
  54. // <body>の<a>内の0番目のタグとtitle属性の値(title)を出力
  55. $at = $html->find('body a',0)->title;
  56. echo $at . “\n”;
  57. // 結果は、linkTitle
  58.  
  59. ?>

サンプルソースの結果をまとめたもの

内容 結果 記述
記述方法パターン1
<b>の0番目のテキスト
hahahahaha $b0 = ('body b',0)->innertext;
記述方法パターン2
<b>の0番目のテキスト
hahahahaha $b3 = ('body b');
echo \b3[0]->innertext;
<b>の1番目のタグとテキスト <b>hehehe</b> $b1 = ('body b',1)->outertext;
<b>の2番目のテキスト he<em>he</em>he $em = ('body b',2)->innertext;
<a>の0番目のhref属性の値 http://recatnap.info/ $a = ('body a',0)->href;
<a>の0番目のtitle属性の値 linkTitle $at = ('body a',0)->title;

参考:HTMLをパースするライブラリ[PHP Simple HTML DOM Parser]

新着(ニュース関連以外)

2018-07-26
年賀状で「新春」とか書くけど・・・何故なんだろうと8月を目前にした今、疑問に思った。
2018-05-16
PHPで画像のヘッダ情報(?)の「Orientation」を元に画像回転させたい。
2018-03-05
Android Studioをインストール。エミュレータを軽くするトコまで終わらせたかったけど、挫折した。
2018-02-23
プッシュ通知について調べてた時にでてきたServiceWorker。そのServiceWorkerについてのメモ。
2017-12-13
jqueryで取得したDOM要素をオブジェクトじゃなくて、配列で受け取りたい