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>[email protected]</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>[email protected]</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>
6
  • Ask for JSON data instead. This one looks pretty ugly. Commented Mar 23, 2012 at 10:02
  • 4
    Have you tried something, or do you simply want someone to write your code for you? Commented Mar 23, 2012 at 10:07
  • 1
    @papaiatis i guess he can't ask 'cause he's just trying to steal some kind of data from another website. Commented Mar 23, 2012 at 10:10
  • see stackoverflow.com/questions/3577641/…. Also see this tutorial net.tutsplus.com/tutorials/php/… on nettutsplus Commented Mar 23, 2012 at 10:10
  • @k102 Yeah, he's probably trying to do that.. Commented Mar 23, 2012 at 10:11

2 Answers 2

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 '[email protected]' (length=24)
    1 => string '[email protected]' (length=24)
    2 => string '[email protected]' (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 :)

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

Comments

0

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

Comments

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.