HTML Table to CSV (inc. Headers and footers) - PHP

I was surfing around the net looking for a way to do this in PHP when I came across this piece of code:

    preg_match('/<table(>| [^>]*>)(.*?)<\/table( |>)/is',$this->raw('./PayrollReportTable.report'),$b);
    $table = $b[2];
    preg_match_all('/<tr(>| [^>]*>)(.*?)<\/tr( |>)/is',$table,$b);
    $rows = $b[2];
    foreach ($rows as $row){
        preg_match_all('/<td(>| [^>]*>)(.*?)<\/td( |>)/is',$row,$b);
        $out[] = strip_tags(implode(',',$b[2]));
    }
    $out = implode("\n", $out);
    var_dump($out);

It does what I want accept for two things, multiple headers and footer are missing from the example as it is designed to work with basic tables. Now I am horrible at Rejex and I am curious if some one could help me out in adding or explaining what the process for adding the headers of a table (this table will have three headers) and the footer (one footer).

the $this->raw('./PayrollReportTable.report') is the actual HTML table to be converted to csv. So far this only converts everything that's not a header and a footer, which is great, cept' I am missing the other components.

Answers


Rather than using preg_match use DOMDocument and DOMXPath.

$domdoc = new DOMDocument();
$domdoc->loadHTML('yourstuff');

$xpath = new DOMXPath($docdoc);


foreach($xpath->query('//tr') as $tr) {
 // etc
}

Need Your Help

Limiting Textfields in Java

java swing

Is there a way to limit a textfield to only allow numbers 0-100, thereby excluding letters, symbols and such as well? I have found a way, but it is way more complicated than seems necessary.

Initializing non-final field

java multithreading java-memory-model

I am currently reading JSR-133 (Java Memory Model) and I can't understand why f.y might be non-initialized (could see 0). Can someone explain it to me?