LoginSignup
0

More than 5 years have passed since last update.

phpqueryで複数のクラスを持つデータを取得する

Posted at

    <?php
    require_once("./phpQuery-onefile.php");
    $html = file_get_contents("https://ja.wikipedia.org/wiki/%E3%82%A6%E3%82%A7%E3%83%96%E3%82%B9%E3%82%AF%E3%83%AC%E3%82%A4%E3%83%94%E3%83%B3%E3%82%B0");

    #ここでhtml全文を取得している
    $phpobj = phpQuery::newDocument($html);

    #classのデータを取得
    #複数のクラスを持つ場合は
    #カンマ区切りでクラスを指定すれば
    #全部の条件に合致するデータを取得できる
    $classes = $phpobj[".asbox, .plainlinks, .noprint"];

    echo "class<br>";

    #それぞれのデータを表示する
    foreach ($classes as $class) {
      echo pq($class)->text()."<br>";
      echo "----------------<br>";
    }
  ?>

カンマで区切れば、複数のクラスを持つ要素でも指定できる。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0