public static function getGeoName($file = COUNTRY_CODE . '.txt') { $searchfor = $_GET['s']; // the following line prevents the browser from parsing this as HTML. header('Content-Type: text/plain'); if (!is_null($searchfor)) { // get the file contents, assuming the file to be readable (and exist) $contents = file_get_contents(__DIR__ . '/GeoNames/' . $file); // escape special characters in the query $pattern = preg_quote($searchfor, '/'); // finalise the regular expression, matching the whole line $pattern = "/^.*{$pattern}.*\$/m"; // search, and store all matching occurences in $matches if (preg_match_all($pattern, $contents, $matches)) { $data = implode("\n", $matches[0]); $array = GeoName::csvToArray($data); } } if (isset($_GET["fc"])) { $result = array_filter($array, function ($k) { return $k->feature_code == $_GET["fc"]; }); } elseif (isset($_GET["a1"])) { $result = array_filter($array, function ($k) { return $k->admin1 == $_GET["a1"]; }); } elseif (isset($_GET["a2"])) { $result = array_filter($array, function ($k) { return $k->admin2 == $_GET["a2"]; }); } else { $result = $array; } if (sizeof($result) > 0) { header('Content-Type: application/json'); echo json_encode($result); } }
continue; } $day = date('Y-m-d', strtotime($match[2])); $header[$key] = array('type' => strtolower($match[1]), 'day' => $day); } return false; } return $row[0]; // return array( // 'timezone_id' => $row[0], // ($header[1]['type'].'_offset') => array( // $header[1]['day'] => $row[1] // ), // ($header[2]['type'].'_offset') => array( // $header[2]['day'] => $row[1], // ), // ); } protected static function _mapCountry($row) { return array('name' => $row[4], 'iso' => $row[0], 'iso3' => $row[1], 'iso_numeric' => $row[2], 'fips' => $row[3], 'captial' => $row[5], 'area' => $row[6], 'population' => $row[7], 'continent' => $row[8], 'tld' => $row[9], 'currency_code' => $row[10], 'currency_name' => $row[11], 'phone_code' => $row[12], 'postal_code_format' => $row[13], 'postal_code_regex' => $row[14], 'languages' => $row[15], 'geoname_id' => $row[16], 'neighbours' => $row[17]); } protected static function _mapCity($row) { return array('geo_name_id' => $row[0], 'name' => $row[1], 'ascii_name' => $row[2], 'alternate_name' => array_filter(preg_split('/(?<!\\\\),/', $row[3])), 'latitude' => $row[4], 'longitude' => $row[5], 'feature_class' => $row[6], 'feature_code' => $row[7], 'country_iso' => $row[8], 'alternate_country_iso' => array_filter(preg_split('/(?<!\\\\),/', $row[9])), 'population' => $row[14], 'average_elevation' => $row[16], 'timezone_id' => $row[17], 'modified' => $row[18]); } } GeoName::generate('time_zones.php'); GeoName::generate('countries.php'); GeoName::generate('big_cities.php');