0

i receive data in this form, i want to parse this html data in to php array.

<table class="qprintable" cellspacing="1" cellpadding="0" border="0" width="600">
<tbody>
<tr>
<td width="300" valign="top">
<table class="qprintable2" cellspacing="0" cellpadding="4" border="0" width="100%">
<tbody>
<tr class="phead">
<td colspan="2">
<b></b>
</td>
</tr>
<tr>
<td valign="top">
<div class="first"></div>
<div></div>
<div>2009jobs.agile@gmail.com</div>
<br>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<br>
<br>
<div></div>
<div class="last"></div>
</td>
<td valign="top">
<div class="first"></div>
<br>
<div></div>
<br>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div class="last">&nbsp;</div>
</td>
</tr>
<tr width="290">
<td valign="top" colspan="2">
<div class="first"></div>
<div></div>
<div class="last"></div>
</td>
</tr>
</tbody>
</table>
</td>
<td width="300" valign="top">
<table class="qprintable2" cellspacing="0" cellpadding="4" border="0" width="100%">
<tbody>
<tr class="phead">
<td colspan="2">
<b></b>
</td>
</tr>
<tr>
<td valign="top">
<div class="first"></div>
<div></div>
<div>aerosoft.career@rediffmail.com</div>
<br>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<br>
<br>
<div></div>
<div class="last"></div>
</td>
<td valign="top">
<div class="first"></div>
<br>
<div></div>
<br>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div class="last">&nbsp;</div>
</td>
</tr>
<tr width="290">
<td valign="top" colspan="2">
<div class="first"></div>
<div></div>
<div class="last"></div>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
Maulik patel
  • 1,551
  • 8
  • 22
  • 44

2 Answers2

1

This one very ugly table ... but if you insest they are 3 solutions

Using http://simplehtmldom.sourceforge.net/

Wrote this simple code :

    var_dump(parseUglyTable($table)) ;
    function parseUglyTable($table)
    {
        $html = str_get_html($table);
        $data = array(); 

        foreach($html->find('tr') as $row) {


            if($row)
            {
                $td = $row->find('td',0);

                $text = str_replace(array("<div>","</div>","&nbsp;"), "\n", $td->plaintext);
                $text = explode("\n", $text);

                foreach($text as $value)
                {
                    $value  = trim($value);
                    if(empty($value))
                        continue ;

                    $data[]  = $value ;
                }
            }
        }
        return $data;
    }

Output

    array
    0 => string '2009jobs.agile@gmail.com' (length=24)
    1 => string '2009jobs.agile@gmail.com' (length=24)
    2 => string 'aerosoft.career@rediffmail.com' (length=30)

Using preg_match_all

I figured that its only email address that exist in that ugly table .... see http://php.net/manual/en/function.preg-match-all.php for more information with is much more efficient

Why use a Tank or Guy to kill a rat ??? just ask for JSON or XML format

I hope this helps

Thanks :)

Baba
  • 94,024
  • 28
  • 166
  • 217
0

Make a string from it and php explode it by "\n"

Jirka Kopřiva
  • 2,939
  • 25
  • 28