0

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?

1
  • The PHPQuery library hasn't been maintained in 4 years. You might want to use more recent alternatives like DOM-Query (github.com/PHPPowertools/DOM-Query). Commented Sep 8, 2015 at 8:01

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks but I really need to use PHPQuery for the task!
What does $rows = pq('ul.im-the-list > li'); do in the code?
that was a leftover that I forgot to remove, you will see that the $rows variable is never used, I will edit it out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.