Parse an HTML Table with PHP

by jl2 on October 14th, 2009

I recently was in the position where I needed to parse a table within an HTML file on a number of different pages.  To save myself some time I wrote this simple script to handle the parsing programatically.

The script will work with most simple tables where the <th> tag has been used to define headers. It is unlikely to work with nested tables! Essentially, it worked for the purposes it was created for but your milage may vary!

function parseTable($html)
  // Find the table
  preg_match("/<table.*?>.*?<\/[\s]*table>/s", $html, $table_html);
  // Get title for each row
  preg_match_all("/<th.*?>(.*?)<\/[\s]*th>/", $table_html[0], $matches);
  $row_headers = $matches[1];
  // Iterate each row
  preg_match_all("/<tr.*?>(.*?)<\/[\s]*tr>/s", $table_html[0], $matches);
  $table = array();
  foreach($matches[1] as $row_html)
    preg_match_all("/<td.*?>(.*?)<\/[\s]*td>/", $row_html, $td_matches);
    $row = array();
    for($i=0; $i<count($td_matches[1]); $i++)
      $td = strip_tags(html_entity_decode($td_matches[1][$i]));
      $row[$row_headers[$i]] = $td;
    if(count($row) > 0)
      $table[] = $row;
  return $table;

Download parseTable.php

Share and Enjoy:
  • email
  • Print
  • Twitter
  • Facebook
  • Digg
  • Technorati
  • StumbleUpon
  • Slashdot
  • LinkedIn
  • Google Bookmarks
  • MySpace
  • Netvibes
  • Reddit
  • Tumblr

From PHP

  1. Derek permalink

    Thank you! I have been all over google looking for a simple version of this that I can use. This is absolutely perfect! This is the first I found with good regex.



  2. jim permalink

    it rocks… thanks

  3. Chals permalink

    please Download “parseTable.php” in some text format


  4. Shouldn’t it be this way?
    preg_match(“/(.*)/s”, $html, $table_html);

  5. whoa! it crashed my regexp )

  6. cool! you are so wonderful~

    it helps me a lot.

    thank you very much!

  7. daniele permalink

    How this works if you have a table in a table?
    I think it doesn’t work…


    will return this exact text: "”

  8. daniele permalink


    table 1
    table 2
    /table 2

    will match: table2

  9. fix: preg_match(“/(.*)?/s”, $html, $table_html);

    Note the (.*)? instead of .*? so that the matches are put into $table_html. Thanks.

  10. graeme permalink

    NM. Now, I see what Marat was saying. The first preg_match needs brackets.

  11. Andreas permalink

    How can i use this function. Can you give me an example please.


  12. Wow! after a 30 minutes of search i got something good.

  13. Dark knight permalink

    Sarah, these are beautiful! Your family clearly have amazing genes.

  14. Dark knight permalink

    Thanks for making me to get new thoughts about desktops. I also have belief that certain of the best ways to help keep your laptop in primary condition has been a hard plastic-type case, and also shell, that suits over the top of one’s computer. These kinds of protective gear tend to be model precise since they are manufactured to fit perfectly within the natural outer shell. You can buy them directly from the owner, or through third party places if they are readily available for your mobile computer, however not every laptop will have a spend on the market. Once again, thanks for your ideas.

  15. olympic london permalink

    Man I would love to have a job like that…

  16. Lurlene Prose permalink

    Andrea! That sounds amazing involving your kids in creative projects is the way to go. Sounds like creativity runs in the veins of your family. I’m super happy you’re enjoying the blog, thanks so much for your compliments comments like this are what makes blogging so so special. Have a wonderful weekend (and do share the outcome of your family collaboration!).

  17. iphone permalink

    Manche schaffen es, andere nicht.

  18. lordrt permalink

    hello, where can I get the php file to download? the link here is sending me to a blank page

  19. zygomatic permalink

    My PHP is a little rusty and I’m a novice for preg functions. Does anyone know a way to skip checking for TH tags if they don’t exist and/or just number the columns/indicies manually? Thanks

  20. paing permalink

    hmmm, cant download the parseTable.php

  21. It’s going to be ending of mine day, however before fknish I
    am reading this grteat piesce of writing to increase my know-how.

  22. For this, ten participants were trained in gaze restricted face recognition in which face masks were viewed through a small aperture controlled by the participant.
    Barbour Annandale Gesteppte Frauen [url=]Barbour Annandale Gesteppte Frauen[/url]

  23. ftig in die Ruder legennursing staffPflegepersonal, das (med), Krankenpflegepersonal,das (jobs)Schwesternschaft, die (Krankenschwestern einer Klinik) (med)Pflegemitarbeiter (pl) (jobs), Pflegekr.
    Belstaff BURGESS PERFECTO Atmungsaktiv [url=]Belstaff BURGESS PERFECTO Atmungsaktiv[/url]

  24. For the visual stimulation condition, we selected the scans with strong visual activation based on a generalized linear model (GLM).
    Belstaff Tragbar BRITANNIA [url=]Belstaff Tragbar BRITANNIA[/url]

  25. ?christian louboutin homme The pygmies live in family groups or clans.
    Barbour Marine Mann [url=]Barbour Marine Mann[/url]

  26. Dass er mit dieser Strategie eine Anonymisierung der B&uuml.
    Bogner Anti-Statik- Rik-T 1108 [url=]Bogner Anti-Statik- Rik-T 1108[/url]

  27. Come on, you Earth, send me your rocket.
    Moncler Anti-Statik- Quentin [url=]Moncler Anti-Statik- Quentin[/url]

  28. Additional loading with Gd3+ can provide MR contrast.
    Moncler Fedor Weinrot [url=]Moncler Fedor Weinrot[/url]

  29. best way to get rid of cellulite no time.
    Moncler lässig DunkelGrün [url=]Moncler lässig DunkelGrün[/url]

  30. In this preliminary work, we begin to study the complex interaction between rendering style and perceived trust, in order to provide guidelines for developers for creating plausible virtual characters.
    Barbour Vintage Gesteppte Innenfutter aus 100% Polyester

  31. Many game developers opt for a stylised rendering (such as cel shading) to avoid the uncanny effect.
    Wellensteyn Kraftstoff Kletterei

  32. We are professionals with great seriousness, it is impossible not to find a common solution that suits both parties.
    Barbour Gilet Lowerdale Gesteppte

  33. Particular attention is dedicated to the human ability to discriminate between motions of different intensity.
    Belstaff Innenfutter aus 100% Polyester RADFORD

  34. 000 Christina: Wie reagiert ihr auf diese Unverschämtheit.
    Belstaff Wolle Baumwolle

  35. In: Proceedings Third Workshop on Non Classical Models for Automata and Applications NCMA 2011, Volume 282.
    Barbour Aviemore Gesteppte Wandern

  36. 40 will be sent via ‘INTERNATIONAL SIGNED FOR’ post to ensure a safe arrival for your high value order.
    Wellensteyn Windschirm Quasar

  37. I want to point out my appreciation for your kindness supporting those people who really need assistance with this.
    Barbour Klettern Valley Wasserdicht

  38. You have to create a function including both of them, which is the interpolation function.
    Belstaff Tinte 2015

  39. For some datasets statistical estimation methods are applied, mostly based on census, migration and vital statistics data.
    Peuterey Gore-Tex Faltbare

  40. The Islamic State’s international attacks call for a strategy.
    Belstaff KIRKHAM Umweltfreundlich

  41. Widespread use of the Guidelines will assist States to overcome the problem of determining the need for reception facilities.
    Belstaff Gore-Tex England

  42. Reiseführer von Iwanowski: Tipps für individuelle Entdecker Dirk Kruse Etzbach und Marita Bromberg von Iwanowski&#039.
    Belstaff Anti-pilling DORRINGTON

  43. vitamin B) and mineralsto give the user a burst of energy.
    Belstaff lässig MANX

  44. 2002) FC: Verizon tells RIAA to shove it: “Should be denied.
    Woolrich Neue Arktis Lady

  45. Achim Preuß in Hamburg gegründet und beschäftigt heute weltweit 250 Mitarbeiterinnen und Mitarbeiter.
    Peuterey Tarnung ArmeeGrün Warm Halten

  46. This will make you tired, moody and someone who is not really excellent to be all around.
    Belstaff 8 Unzen Gewachster Baumwolle Bequem

  47. (19 06 15)Reynaldo: Have you got any.
    Moncler Folding Lionel

  48. Bonjour j’ai eu à faire à un acheteur d’or ambulant dans un grand centre commercial cette semaine il me propose 15e le gramme de 18 carat je me pose des question en vu de la valeur afficher si dessus 22,38 merci de bien vouloir m’aiguiller vers un acheteur honnête je suis du 49 cordialement gio

  49. A debt of gratitude is in order for making me to get new musings about desktops. I additionally have conviction that sure of the most ideal approaches to help keep your portable workstation in essential condition has been a hard plastic-sort case, and furthermore shell, that suits over the highest point of one’s PC. These sorts of defensive rigging have a tendency to be show exact since they are made to fit consummately inside the regular external shell. You can get them straightforwardly from the proprietor, or through outsider spots on the off chance that they are promptly accessible for your portable PC, however not each tablet will have a spend available. By and by, a debt of gratitude is in order for your thoughts.

Trackbacks & Pingbacks

  1. Why use MySQL instead of a HTML table? [closed] |
  2. Parse html table using file_get_contents to php array - Tech Magazine

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS