php - PHPQuery- Select table columns into an array -


i have html file table this:

<tr valign="top" class="dselbkg" onmouseover="this.classname='selbkg'" onmouseout="this.classname='dselbkg'" >      <td height="20" align="center">1</td>     <td height="20"><div align="center">16-12-2014</div></td>     <td ><div align="center">1st<br>             (10:0 - 1:0 pm)</div></td>     <td ><div align="center">be2105 </div></td>     <td >programming in c</td> </tr>  <tr valign="top" class="dselbkg" onmouseover="this.classname='selbkg'" onmouseout="this.classname='dselbkg'" >      <td height="20" align="center">2</td>     <td height="20"><div align="center">18-12-2014</div></td>     <td ><div align="center">1st<br>             (10:0 - 1:0 pm)</div></td>     <td ><div align="center">bs1101 </div></td>     <td >mathematics - i</td> </tr>  <tr valign="top" class="dselbkg" onmouseover="this.classname='selbkg'" onmouseout="this.classname='dselbkg'" >      <td height="20" align="center">3</td>     <td height="20"><div align="center">20-12-2014</div></td>     <td ><div align="center">1st<br>             (10:0 - 1:0 pm)</div></td>     <td ><div align="center">hm3101 </div></td>     <td >communicative english</td> </tr>  

i need each <td> separate element of array based on class id.

i using phpquery. tried

$table = $all['tr.dselbkg']; $columns = $table['td']; 

but puts columns single string. $columns[0] prints out columns while $columns[1] blank.

how can take individual <td> single element in array?

edit using phpquery:

<?php  include('phpquery.php');  $htmlstring = '<tr valign="top" class="dselbkg" onmouseover="this.classname=\'selbkg\'" onmouseout="this.classname=\'dselbkg\'" >      <td height="20" align="center">1</td>     <td height="20"><div align="center">16-12-2014</div></td>     <td ><div align="center">1st<br>             (10:0 - 1:0 pm)</div></td>     <td ><div align="center">be2105 </div></td>     <td >programming in c</td> </tr>  <tr valign="top" class="dselbkg" onmouseover="this.classname=\'selbkg\'" onmouseout="this.classname=\'dselbkg\'" >      <td height="20" align="center">2</td>     <td height="20"><div align="center">18-12-2014</div></td>     <td ><div align="center">1st<br>             (10:0 - 1:0 pm)</div></td>     <td ><div align="center">bs1101 </div></td>     <td >mathematics - i</td> </tr>  <tr valign="top" class="dselbkg" onmouseover="this.classname=\'selbkg\'" onmouseout="this.classname=\'dselbkg\'" >      <td height="20" align="center">3</td>     <td height="20"><div align="center">20-12-2014</div></td>     <td ><div align="center">1st<br>             (10:0 - 1:0 pm)</div></td>     <td ><div align="center">hm3101 </div></td>     <td >communicative english</td> </tr> ';  $doc = phpquery::newdocumenthtml($htmlstring);  foreach (pq('tr.dselbkg') $row){     $columns = array();     foreach(pq('td',$row) $td) $columns[] = $td->nodevalue;     $tablerows[] = $columns; }  print_r($tablerows);  /* array (     [0] => array         (             [0] => 1             [1] => 16-12-2014             [2] => 1st             (10:0 - 1:0 pm)             [3] => be2105             [4] => programming in c         )      [1] => array         (             [0] => 2             [1] => 18-12-2014             [2] => 1st             (10:0 - 1:0 pm)             [3] => bs1101             [4] => mathematics -         )      [2] => array         (             [0] => 3             [1] => 20-12-2014             [2] => 1st             (10:0 - 1:0 pm)             [3] => hm3101             [4] => communicative english         )  )  */ 

you can use simple html dom query html.

here's example how build array parsed td elements.

<?php  include('simple_html_dom.php');  $htmlstring = '<tr valign="top" class="dselbkg" onmouseover="this.classname=\'selbkg\'" onmouseout="this.classname=\'dselbkg\'" >      <td height="20" align="center">1</td>     <td height="20"><div align="center">16-12-2014</div></td>     <td ><div align="center">1st<br>             (10:0 - 1:0 pm)</div></td>     <td ><div align="center">be2105 </div></td>     <td >programming in c</td> </tr>  <tr valign="top" class="dselbkg" onmouseover="this.classname=\'selbkg\'" onmouseout="this.classname=\'dselbkg\'" >      <td height="20" align="center">2</td>     <td height="20"><div align="center">18-12-2014</div></td>     <td ><div align="center">1st<br>             (10:0 - 1:0 pm)</div></td>     <td ><div align="center">bs1101 </div></td>     <td >mathematics - i</td> </tr>  <tr valign="top" class="dselbkg" onmouseover="this.classname=\'selbkg\'" onmouseout="this.classname=\'dselbkg\'" >      <td height="20" align="center">3</td>     <td height="20"><div align="center">20-12-2014</div></td>     <td ><div align="center">1st<br>             (10:0 - 1:0 pm)</div></td>     <td ><div align="center">hm3101 </div></td>     <td >communicative english</td> </tr> ';  $html = str_get_html($htmlstring);  foreach($html->find('tr.dselbkg') $tr){     $columns = array();      foreach($tr->find('td') $td)         $columns[] = $td->outertext; // outertext if want full td tag         // $columns[] = $td->innertext // innertext if want text inside td tag      $tablerows[]=$columns; }  print_r($tablerows);  /*  array (     [0] => array         (             [0] => <td height="20" align="center">1</td>             [1] => <td height="20"><div align="center">16-12-2014</div></td>             [2] => <td ><div align="center">1st<br>              (10:0 - 1:0                           pm)</div></td>             [3] => <td ><div align="center">be2105 </div></td>             [4] => <td >programming in c</td>         )      [1] => array         (             [0] => <td height="20" align="center">2</td>             [1] => <td height="20"><div align="center">18-12-2014</div></td>             [2] => <td ><div align="center">1st<br>              (10:0 - 1:0                           pm)</div></td>             [3] => <td ><div align="center">bs1101 </div></td>             [4] => <td >mathematics - i</td>         ) */  $html->clear(); unset($html); 

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -