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


how can text html table cells using php dom query?

html table is:

<table>   <tr>     <th>job location:</th>     <td><a href="/#">kabul</a>     </td>   </tr>   <tr>     <th>nationality:</th>     <td>afghan</td>   </tr>   <tr>     <th>category:</th>     <td>program</td>   </tr> </table> 

i have following query doesn't work:

$xmlpagedom = new domdocument(); @$xmlpagedom->loadhtml($html); $xmlpagexpath = new domxpath($xmlpagedom); $value = $xmlpagexpath->query('//table td /text()'); 

get complete table php domdocument , print it

the answer this:

$html = "<table id='myid'><tr><td>1</td><td>2</td></tr><tr><td>4</td><td>5</td></tr><tr><td>7</td><td>8</td></tr></table>";  $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 $row) {     $cells = $row -> getelementsbytagname('td');     foreach ($cells $cell) {         print $cell->nodevalue;     } } 

edit: use instead

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

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -