How to select text from HTML table using PHP DOM query?

后端 未结 1 1721
感情败类
感情败类 2021-01-26 10:00

How can I get text from HTML table cells using PHP DOM query?

HTML table is:

Job Location:
1条回答
  •  盖世英雄少女心
    2021-01-26 10:51

    get a complete table with php domdocument and print it

    The answer is like this:

    $html = "
    12
    45
    78
    "; $xml = new DOMDocument(); $xml->validateOnParse = true; $xml->loadHTML($html); $xpath = new DOMXPath($xml); $table =$xpath->query("//*[@id='myid']")->item(0); $rows = $table->getElementsByTagName("tr"); foreach ($rows as $row) { $cells = $row -> getElementsByTagName('td'); foreach ($cells as $cell) { print $cell->nodeValue; } }

    EDIT: Use this instead

    $table = $xpath->query("//table")->item(0);
    

    0 讨论(0)
提交回复
热议问题