<?php
set_time_limit(0);

// template for pages

	$fh1 = fopen('template.csv','r');
	while(!feof($fh1))
	{
		$lines[] = fgetcsv($fh1, 1024);
	}
	fclose($fh1);
	
	foreach($lines as $line)
	{
		if(is_array($line))
			$cont[] = implode(",",$line);
		else
			$cont[] = $line;
	}
	
	$original_content = implode("\r\n",$cont);

// Create pages	
	$file_handle = fopen('separate-regions.csv', 'r');
	
	$new_content = "Campaign,Campaign Daily Budget,Languages,Geo Targeting,Proximity Targets,Ad Schedule,Ad Group,Max CPC,Display Network Max CPC,Placement Max CPC,Max CPM,CPA Bid,Keyword,Website,Keyword Type,First Page CPC,Quality Score,Headline,Description Line 1,Description Line 2,Ad Name,Image Size,Display URL,Destination URL,Campaign Status,AdGroup Status,Creative Status,Keyword Status,Website Status,Suggested Changes,Comment\r\n";

	while(!feof($file_handle)) 
	{
		$line 		= fgetcsv($file_handle, 1024);
		$city 		= $line[0];
		$code		= $line[1];
		$oldpage	= '/0161-manchester-uk-numbers.php';
		$pagename 	= '/0'.$code.'-'.strtolower(str_replace("- ","",str_replace(" ","-",str_replace("(","",str_replace(")","",$city))))).'-uk-numbers.php';
				
		$new_content .= str_replace("161",$code,str_replace("Manchester",$city,str_replace($oldpage, $pagename, $original_content)))."\r\n";
	}

	fclose($file_handle);
	
	header("Content-type: text/csv");
	header('Content-Disposition: attachment; filename="generated-adwords.csv"');
	echo $new_content; 
	exit();
?>