I have a HTML file like with a table like 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 AM - 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 AM - 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 AM - 1:0 PM)</div></td>
<td ><div align="center">HM3101 </div></td>
<td >COMMUNICATIVE ENGLISH</td>
</tr>
I need to get each <td>
into a separate element of an array based on the class id.
I am using PHPQuery. I tried
$table = $all['tr.dselbkg'];
$columns = $table['td'];
But what it does is puts all the columns as a single string. So $columns[0]
prints out all the columns while $columns[1]
is blank.
How can I take individual <td>
as a single element in an 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 AM - 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 AM - 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 AM - 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') as $row){
$columns = array();
foreach(pq('td',$row) as $td) $columns[] = $td->nodeValue;
$tableRows[] = $columns;
}
print_r($tableRows);
/*
Array
(
[0] => Array
(
[0] => 1
[1] => 16-12-2014
[2] => 1ST
(10:0 AM - 1:0 PM)
[3] => BE2105
[4] => PROGRAMMING IN C
)
[1] => Array
(
[0] => 2
[1] => 18-12-2014
[2] => 1ST
(10:0 AM - 1:0 PM)
[3] => BS1101
[4] => MATHEMATICS - I
)
[2] => Array
(
[0] => 3
[1] => 20-12-2014
[2] => 1ST
(10:0 AM - 1:0 PM)
[3] => HM3101
[4] => COMMUNICATIVE ENGLISH
)
)
*/
You can easily use simple html dom to query your html.
Here's an example how to build an array from 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 AM - 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 AM - 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 AM - 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') as $tr){
$columns = array();
foreach($tr->find('td') as $td)
$columns[] = $td->outertext; // outertext if you want the full td tag
// $columns[] = $td->innertext // innertext if you just want the text inside the 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 AM - 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 AM - 1:0 PM)</div></td>
[3] => <td ><div align="center">BS1101 </div></td>
[4] => <td >MATHEMATICS - I</td>
)
*/
$html->clear();
unset($html);