1

I want to generate CSV File from HTML table which contains logo image and other invoice data in $content i have invoice html data in table format. I have tried this displaying with  

$csv = array();
preg_match('/<table(>| [^>]*>)(.*?)<\/table( |>)/is',$content,$b);
$table = $b[2];
preg_match_all('/<tr(>| [^>]*>)(.*?)<\/tr( |>)/is',$content,$b);
$rows = $b[2];
foreach ($rows as $row) {
    //cycle through each row
    if(preg_match('/<th(>| [^>]*>)(.*?)<\/th( |>)/is',$row)) {
        //match for table headers
        preg_match_all('/<th(>| [^>]*>)(.*?)<\/th( |>)/is',$row,$b);
        $csv[] = strip_tags(implode(',',$b[2]));
    } elseif(preg_match('/<td(>| [^>]*>)(.*?)<\/td( |>)/is',$row)) {
        //match for table cells
        preg_match_all('/<td(>| [^>]*>)(.*?)<\/td( |>)/is',$row,$b);
        $csv[] = strip_tags(implode(',',$b[2]));
    }
}
$csv = implode("\n", $csv);

$furl="C:\WTServer\WWW/test.txt";
file_put_contents($furl,$csv);
mayank
  • 442
  • 4
  • 12
  • 1
    I'm half tempted to vote to close this as a [duplicate](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454)... use a [DOM parser](http://php.net/manual/en/class.domdocument.php). – CD001 Jul 31 '17 at 15:34
  • @CD001 Thanks for your answer but i don't want to use library. – mayank Aug 01 '17 at 14:50

0 Answers0