<?php
set_time_limit(0);

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

	while(!feof($file_handle)) 
	{
		$line 	= fgetcsv($file_handle, 1024);
		
		$city 	= $line[0];
		$code	= $line[1];
		$city_all = $line[2];
		$city_link = '/numbers/0'.$code.'-'.strtolower(str_replace(" ","-",str_replace("(","",str_replace(")","",$city)))).'-uk-numbers.php';
		
		// Add entry
		chdir('/home/fdesign1/public_html/phone-numbers-uk.com/numbers/');
		
		
	// Add city link
		$regions_file = '/home/fdesign1/public_html/phone-numbers-uk.com/numbers/uk-regional-numbers.php';
		$matchA = 'End A';
		$matchC = 'End C';
		$matchG = 'End G';
		$matchL = 'End L';
		$matchO = 'End O';
		$matchT = 'End T';
		$country_link = '<a href="'.$city_link.'">'.$city_all.' hotels</a><br/>';
		$country_cont="";
		
		$string = file($regions_file);
		
		if($city[0] == 'A' || $city[0] == 'B')
		{
			foreach($string as $country_line)
			{
				if(strstr($country_line,$matchA))
				{
					$country_cont .= $country_link."\n".$country_line;
				} else
				{
					$country_cont .= $country_line;
				}
			}
		}else if($city[0] == 'C' || $city[0] == 'D' || $city[0] == 'E' || $city[0] == 'F')
		{
			foreach($string as $country_line)
			{
				if(strpos($country_line,$matchC))
				{
					$country_cont .= $country_link."\n".$country_line;
				} else
				{
					$country_cont .= $country_line;
				}
			}
		}else if($city[0] == 'G' || $city[0] == 'H' || $city[0] == 'I' || $city[0] == 'J' || $city[0] == 'K')
		{
			foreach($string as $country_line)
			{
				if(strpos($country_line,$matchG))
				{
					$country_cont .= $country_link."\n".$country_line;
				} else
				{
					$country_cont .= $country_line;
				}
			}
		}else if($city[0] == 'L' || $city[0] == 'M' || $city[0] == 'N')
		{
			foreach($string as $country_line)
			{
				if(strpos($country_line,$matchL))
				{
					$country_cont .= $country_link."\n".$country_line;
				} else
				{
					$country_cont .= $country_line;
				}
			}
		} else if($city[0] == 'O' || $city[0] == 'P' || $city[0] == 'Q' || $city[0] == 'R' || $city[0] == 'S')
		{
			foreach($string as $country_line)
			{
				if(strpos($country_line,$matchO))
				{
					$country_cont .= $country_link."\n".$country_line;
				} else
				{
					$country_cont .= $country_line;
				}
			}
		} else if($city[0] == 'T' || $city[0] == 'U' || $city[0] == 'V' || $city[0] == 'W' || $city[0] == 'X' || $city[0] == 'Y' || $city[0] == 'Z')
		{
			foreach($string as $country_line)
			{
				if(strpos($country_line,$matchT))
				{
					$country_cont .= $country_link."\n".$country_line;
				} else
				{
					$country_cont .= $country_line;
				}
			}
		}
		
		if ($handle_country = fopen($regions_file,'w'))
		{
			fwrite($handle_country,$country_cont);
			fclose($handle_country);
		}

	
	}

	fclose($file_handle);
	echo 'Files created.';
?>