<?php
set_time_limit(0);

// template for pages

	$fh1 = fopen('adwords-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 new csv file
	$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,Keyword Type,First Page CPC,Quality Score,Headline,Description Line 1,Description Line 2,Display URL,Destination URL,Campaign Status,AdGroup Status,Creative Status,Keyword Status,Suggested Changes,Comment\r\n";

	while(!feof($file_handle)) 
	{
		$line 		= fgetcsv($file_handle, 1024);
		$city 		= $line[0];
		$code		= $line[1];
				
		$new_content .= str_ireplace("208",$code,str_ireplace("London",$city,$original_content))."\r\n";
	}

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

?>