Generate multiple HTML for exactly same layout from excel .CSV with PHP

Previously I have a project where I will need to create a large amount of HTML files from the excel given by my clients. So I thought, maybe it could help others with the same issue. Cut down your working time without having to copy paste the content one by one from the excel. Of course, the same concept can work well if you need to create files from your database records.

filestructure

Your directory should looks something like this. A generated_html folder to save all the html files to, an index.php to run the generate codes, a CSV files to read the record from, and a template html to use for duplication.

 

Prepare the template HTML

You’ll need a HTML to work as a template, in this template we will replace the data that is different in each files.  In this tutorial, I’ll keep it simple. Create a new template.html file, in this html you need to have 3 elements:

{name}

{address}

{description}

You may put them into <div> or a paragraph based on your design needs. This 3 elements will be use as a keyword to replace the necessary details into whenever they need to be in.

 

Your Excel File

Your excel to be read has to be in .csv form. In this sample, I’ll have 3 columns in the csv file.

Name Address Description
AAAA No1, Street AAA, Canada There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form…
BBB No2, Street BBB, Canada The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested.

PHP code to generate files

Include the following codes in your index.php

$base_template = file_get_contents('template.html');
$file_to_read = 'sample.csv';

ini_set("auto_detect_line_endings", 1);	
$filename = '';

if (($handle = fopen($file_to_read, "r")) !== FALSE) {
	while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
		$name = $data[0];
		$address = $data[1];
		$description = $data[2];
		
		$filename = preg_replace('/[\s\W]+/','',$name);
		$filename =  substr($filename, 0, 10);

		$template = str_replace('{name}', $name, $base_template);
		$template = str_replace('{address}', $address, $template);
		$template = str_replace('{description}', $description, $template);

		$file = fopen('generated_html/'.$filename.'.html',"w+");
		fwrite($file, $template);	
		fclose($file);
	}
	fclose($handle);
}

$base_template is the template.html we created earlier and $file_to_read the excel file we’ll use to retrieve the data to write into the new html.

ini_set("auto_detect_line_endings", 1);

Can be omit if you already have this in your php.ini setting. It’s use to detect the the row in your excel file. The continue lines are the code to read the excel file and run each row in a while loop.

You can grap the data from the excel with [number of column – 1], lets say your name is in the first column, you’ll need to retrieve it with [0]. Remember, always minus 1. You can have more than 3 columns of course, just make sure you put the data at the right place.

$filename = preg_replace('/[\s\W]+/','',$name);
$filename =  substr($filename, 0, 10);

We’ll create a filename with the name by removing all the symbol in it, and only take the first 10 letters from the name to use as a filename. You can remove the second line if you want to use the full name as filename.

Then finally, we have all the value ready, and can replace the elements into proper information with the following code:

$template = str_replace('{name}', $name, $base_template);
$template = str_replace('{address}', $address, $template);
$template = str_replace('{description}', $description, $template);

Lastly, we need to create the HTML file if it does not exists, or replace the data with new details if it already exists with w+ in fopen.

$file = fopen('generated_html/'.$filename.'.html',"w+");
fwrite($file, $template);	
fclose($file);

Close the file cache after finish.  You may refer to the link here for more fopen type. Now you can go to the generated_html folder to get your files.

 

I get an error message of “couldn’t write to file” or “cannot create files”

If you’re not running this code in your localhost, you might face the problem. Make sure you’ve set all permission to read+write (777) for generated_html folders and it should solve the problem.

 

Click here to download the example files

Leave a Reply

Your email address will not be published. Required fields are marked *