0

I am making a php app to parse HTML contents. I need to store a certain table column in php variables.

Here is my code:

$dom = new domDocument;

@$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$tables = $dom->getElementsByTagName('table');

    $rows = $tables->item(0)->getElementsByTagName('tr');    
    $flag=0;
    foreach ($rows as $row)
    {
            if($flag==0) $flag=1;
            else
            {
                    $cols = $row->getElementsByTagName('td');
                    foreach ($cols as $col)
                    {
                        echo $col->nodeValue; //NEED HELP HERE
                    }
                    echo '<hr />';
            }
     }

In each row, first col is the KEY, second is the VALUE. How to create key value pairs from the table and store them as arrays in php.

I tried many things but everytime I am just getting DOMElement Object() as value.

Any help is deeply appreciated...

HTML as requested:

<table align='center' border='0' cellpadding='0' cellspacing='0' style='border-collapse: collapse' width='780' height=100%>
<tr><td height=96% align=center><BR><BR>    
<html>
<head>
</head>
<body style="background:url(uptu_logo1.gif); background-repeat:no-repeat; background-position:center">
<p align="center" style="font-size:18px"><span style='font-size:20px'>this text is unimportant gibberish that is not required by my app</span><br/><span style='font-size:16px'>this text is unimportant gibberish that is not required by my app</span><br/><u>B.Tech. Third Year Result 2009-10. this text is unimportant gibberish that is not required by my app</u></p>
<br/>
<table align="center" border="1" cellpadding="0" cellspacing="0" bordercolor="#E3DDD5" width="700" style="border-collapse: collapse; font-size: 11px">
<tr>

<td width="50%"><b>Name:</b></td>
<td width="50%">John Fernandes          </td>
</tr>
<tr>
<td><b>Fathers Name:</b></td>
<td>Caith Fernandes                 </td>
</tr>
<tr>
<td><b>Roll No:</b></td>
<td>0702410099</td>
</tr>

<tr>
<td><b>Status:</b></td>
<td>REGULAR   </td>
</tr>
<tr>
<td><b>Course/Branch:</b></td>
<td>B. Tech. </td>
</tr>
<tr>
<td><b>Institute Name</b></td>
<td>Imperial College of Science and Technology</td>

</tr>
</table>

My PHP code outputs:

Name:John Fernandes          <hr />
Fathers Name:Caith Fernandes                 <hr />
Roll No:0702410099<hr />
Status:REGULAR   <hr />
Course/Branch:B. Tech. Computer Science and Engineering (10)<hr />
Imperial College of Science and Technology<hr />

Also how to get rid of this silly  ? I saw   in the original HTML so I tried to sanitize using PHP function html_entity_decode() But its still there...

5
  • Shouldn't $dom = new domDocument; be $dom = new DOMDocument();? Commented Mar 2, 2011 at 17:23
  • 1
    @Rocket Class names in PHP are case-insensitive, and the brackets are optional on constructors without any arguments. Commented Mar 2, 2011 at 17:25
  • 1
    Can you include the HTML? Also, is cols = $row->getElementsByTagName('td'); always going to return just 2 columns? Commented Mar 2, 2011 at 17:25
  • @Rocket, Its working without () Commented Mar 2, 2011 at 17:26
  • @McHerbie I have added the HTML. Commented Mar 2, 2011 at 17:38

1 Answer 1

2

What is the HTML that you are loading? I am assuming that it's something simple like so:

<table>
    <tr>
        <td>heading</td>
        <td>heading</td>
    </tr>
    <tr>
        <td>key</td>
        <td>value</td>
    </tr>
</table>

Looks like the first tr is skipped (the headings), and then you have just 2 columns that you want to pair up as KEY => VALUE;

$cols = $row->getElementsByTagName('td');
$key = $cols->item(0)->nodeValue; // string(3) "key"
$val = $cols->item(1)->nodeValue; // string(5) "value"

The above code will return the items you want.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 This is my assumption too. With your example you could go for $cols = $row->childNodes though.

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.