从文本文件中挑选国家/地区代码和相应的域

I have a text file like:

en-US -> www.google.co.uk
en-UK -> www.bbc.co.uk
es-CO -> www.somedomain.com

I want to be able to pick out the country/language type for example en-US and the corresponding domain name on the same line. How do I do this?

A start:

 $file = "data.txt";
 $f = fopen($file, "r");

 while ( $line = fgets($f, 1000) ) {

   if (preg_match('/^-> .*$/', $line)) {
      fwrite($out, $fh);
   }
   print $line;
 }
list($code, $domain) = array_map('trim', explode('->', $line, 2));
  • Split line by -> into two parts
  • Remove leading / ending spaces
  • Extract as variables

Why do that when you could just use an existing format and get better control:

Ini file

en_US = www.google.co.uk
en_UK = www.bbc.co.uk
es_CO = www.somedomain.com

PHP

$domains = parse_ini_file('data.ini');

print_r($domains);

PHP Output

Array(
  'en_US' => 'www.google.co.uk',
  'en_UK' => 'www.bbc.co.uk',
  'es_CO' => 'www.somedomain.com'
)