14
Sep '12

Converting an HTML Table to CSV in PHP

With some cheeky regular expressions, converting an HTML table to CSV is actually very simple and efficient. Starting with the raw table markup in the variable $table_string you just need to do the following to store it as a CSV file:

$handle = fopen('output.csv', 'w');

if(preg_match_all('/<tr.*?>(.*?)<\/[\s]*tr>/s', $table_string, $row_matches)) {
	foreach($row_matches[1] as $product) {
		if(preg_match_all('/<t[dh].*?>(.*?)<\/[\s]*t[dh]>/s', $product, $data_matches)) {
			$product_data = array();

			foreach($data_matches[1] as $data) {
				$product_data[] = strip_tags($data);
			}

    			fputcsv($handle, $product_data);
		}
	}
}

fclose($handle);

Leave a Reply