#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    3
    Rep Power
    0

    How to Extract data from html table?


    How to Extract data from html table? I have html source stored in a variable and want extract data from the table... Please, help me...
  2. #2
  3. (retired)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2003
    Location
    The Laboratory
    Posts
    10,054
    Rep Power
    1390
    Hi,

    Have you tried using explode?

    I'm assuming you've got the HTML into a variable, so this shouldn't be too hard :-).

    Right as you know in HTML, each table entry is enclosed by <td> xxxx </td>. However, there's going to be all sorts of other tags in there which we don't want (inlcuding things like <table>, <tr>, </tr>, and </td> (this last one doesn't really help us) so we need to do two things:

    First: strip all HTML tags except our <TD> delimiter (& assuming your data's in the variable $html):

    $html = strip_tags($html, '<TD>');

    Second - explode the $html data:

    $delimiter = '<td>';
    $htmldata = explode($delimiter, $html);

    This will make a nice array, where each table entry has it's own entry.

    Hope this works!
    Simon
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    3
    Rep Power
    0
    some of <td> are <td ALIGN=CENTER> or <td nowrap>... Is it possible to do it with eregi() and how?
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Malaysia
    Posts
    138
    Rep Power
    15
    I though if you could clear the alignment and nowrap argument first...like this:

    PHP Code:
    $html strip_tags($html'<TD>');
    $html preg_replace("/(<td)(.*?)(>)/i""$1$3"$html);

    $delimiter '<td>';
    $htmldata explode($delimiter$html); 
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    3
    Rep Power
    0
    10x, I have one more question - is it possible to be done with an array like this $table[row][cell], because now i'm not sure where the new row begins?
    Last edited by zlozi; January 17th, 2004 at 09:24 AM.

IMN logo majestic logo threadwatch logo seochat tools logo