16

I am trying to parse the table shown here into a multi-dimensional php array. I am using the following code but for some reason its returning an empty array. After searching around on the web, I found this site which is where I got the parseTable() function from. From reading the comments on that website, I see that the function works perfectly. So I'm assuming there is something wrong with the way I'm getting the HTML code from file_get_contents(). Any thoughts on what I'm doing wrong?

<?php

$data = file_get_contents('http://flow935.com/playlist/flowhis.HTM');

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;
}

$output = parseTable($data);

print_r($output);

?>

I want my output array to look something like this:

1
--> 11:33AM
--> DEV
--> IN THE DARK

2
--> 11:29AM
--> LIL' WAYNE
--> SHE WILL

3
--> 11:26AM
--> KARDINAL OFFISHALL
--> NUMBA 1 (TIDE IS HIGH)
2
  • 1
    -1 for lack of effort. Isolate your problem instead of basically posting a huge block of code and asking people to figure out what's wrong and fix it. Commented Nov 27, 2011 at 16:50
  • This minimal reproducible example has been destroyed by consequence of the sample input link being dead. Commented Nov 7, 2023 at 0:31

2 Answers 2

51

Don't cripple yourself parsing HTML with regexps! Instead, let an HTML parser library worry about the structure of the markup for you.

I suggest you to check out Simple HTML DOM (http://simplehtmldom.sourceforge.net/). It is a library specifically written to aid in solving this kind of web scraping problems in PHP. By using such a library, you can write your scraping in much less lines of code without worrying about creating working regexps.

In principle, with Simple HTML DOM you just write something like:

$html = file_get_html('http://flow935.com/playlist/flowhis.HTM');
foreach($html->find('tr') as $row) {
   // Parse table row here
}

This can be then extended to capture your data in some format, for instance to create an array of artists and corresponding titles as:

<?php
require('simple_html_dom.php');

$table = array();

$html = file_get_html('http://flow935.com/playlist/flowhis.HTM');
foreach($html->find('tr') as $row) {
    $time = $row->find('td',0)->plaintext;
    $artist = $row->find('td',1)->plaintext;
    $title = $row->find('td',2)->plaintext;

    $table[$artist][$title] = true;
}

echo '<pre>';
print_r($table);
echo '</pre>';

?>

We can see that this code can be (trivially) changed to reformat the data in any other way as well.

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

6 Comments

That worked perfectly. But I need to make a multi dimensional array as shown at the bottom of the original question.
Did you check out example "Scraping Slashdot!" from the simplehtmldom site? As far as I understand it answer to that question.
Ok I added a further example, but this is as far as I will go. I will leave the rest for you to figure out.
Yes I figured it out. Thanks for the further example.
Using simplehtmldom instead of rexexps is fun, isn't it? :)
|
22

I tried simple_html_dom but on larger files and on repeat calls to the function I am getting zend_mm_heap_corrupted on php 5.3 (GAH). I have also tried preg_match_all (but this has been failing on a larger file (5000) lines of html, which was only about 400 rows of my HTML table.

I am using this and its working fast and not spitting errors.

$dom = new DOMDocument();  

//load the html  
$html = $dom->loadHTMLFile("htmltable.html");  

  //discard white space   
$dom->preserveWhiteSpace = false;   

  //the table by its tag name  
$tables = $dom->getElementsByTagName('table');   


    //get all rows from the table  
$rows = $tables->item(0)->getElementsByTagName('tr');   
  // get each column by tag name  
$cols = $rows->item(0)->getElementsByTagName('th');   
$row_headers = NULL;
foreach ($cols as $node) {
    //print $node->nodeValue."\n";   
    $row_headers[] = $node->nodeValue;
}   

$table = array();
  //get all rows from the table  
$rows = $tables->item(0)->getElementsByTagName('tr');   
foreach ($rows as $row)   
{   
   // get each column by tag name  
    $cols = $row->getElementsByTagName('td');   
    $row = array();
    $i=0;
    foreach ($cols as $node) {
        # code...
        //print $node->nodeValue."\n";   
        if($row_headers==NULL)
            $row[] = $node->nodeValue;
        else
            $row[$row_headers[$i]] = $node->nodeValue;
        $i++;
    }   
    $table[] = $row;
}   

var_dump($table);

This code worked well for me. Example of original code is here.

http://techgossipz.blogspot.co.nz/2010/02/how-to-parse-html-using-dom-with-php.html

2 Comments

you should do array_shift($table) because first element will be [0] => array(0) { } empty. it's because you get all tr tags in $rows including the one with th tags. I will suggest an edit.
I am using DOM with PHP 5.6.31, but found that the result of $rows = $tables->item(0)->getElementsByTagName('tr') does not contain any <td> tags for a subsequent $cols = $row->getElementsByTagName('td'). Any idea why my first call to getElementsByTagName() appears to be stripping the HTML tags?

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.