<?php
set_time_limit(0);

// template for pages

	$original_content = "";
	$fh1 = fopen('template.php','r');
	while(!feof($fh1))
	{
		$original_content .= fgets($fh1);
	}
	fclose($fh1);

// Create pages	
	$file_handle = fopen('separate-regions-ll.csv', 'r');

	while(!feof($file_handle)) 
	{
		$line 		= fgetcsv($file_handle, 1024, ';');
		$city 		= trim($line[0]);
		$code		= '0'.trim($line[1]);
		$nozerocode = trim($line[1]);
		$city_text	= trim($line[2]);
		$latlong	= trim($line[3]);
				
		if(!empty($line[4]))
			$suburbs	= '<p><strong>Suburbs in the 01200 area code include:</strong><br/> '.trim($line[4]).'</p>';
		else 
			$suburbs	= '';
		
		$other_areas = $line[5];
		$markers = str_replace("@",";",$line[6]);
		$bmarkers = str_replace("@",";",$line[7]);
		
		$city_file 	= $code.'-numbers-'.strtolower(str_replace("- ","",str_replace(")","",str_replace("(","",str_replace(" ","-",$city))))).'.php';
		
		// Make city page
//        chdir('D://wamp/www/pnuk-ecoms/geographic-numbers/');
		chdir('/home1/fdesign1/public_html/virteluk.com/geographic-numbers/');
		
		if(file_exists($city_file))
		{
			unlink($city_file);
			echo $city_text.' original file deleted.<br>';
		}
		
		
		$new_content = str_replace("// add other markers here",$bmarkers,str_replace("// add markers here",$markers,str_replace("XXOtherAreasXX",$other_areas,str_replace("1200",$nozerocode,str_replace("Clitheroe",$city,str_replace('53.87278,-2.391749',$latlong,str_replace('XXSuburbsXX',$suburbs,$original_content)))))));
		
		$fh = fopen($city_file,'w');
		if(fwrite($fh,$new_content))
		{
			echo $city_file.' file created.<br>';
		}
		fclose($fh);
	
	}

	fclose($file_handle);
	echo '<br><b>All Files have been updated.</b>';
?>