Example #1
0
 public static function SearchFor($criteria, $file = COUNTRY_CODE)
 {
     $file = $file . '.txt';
     $array = null;
     if (!is_null($criteria)) {
         $contents = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'GeoNames' . DIRECTORY_SEPARATOR . $file);
         $pattern = preg_quote($criteria, '/');
         $pattern = "/^.*{$pattern}.*\$/m";
         if (preg_match_all($pattern, $contents, $matches)) {
             $data = implode("\n", $matches[0]);
             $array = GeoNames::csvToArray($data);
         }
     }
     if (sizeof($array) > 0) {
         return $array;
     }
 }
Example #2
0
 public static function SearchFor($criteria, $id = NULL)
 {
     $file = COUNTRY_CODE . '.txt';
     $array = null;
     $result = null;
     GeoNames::$ID_ARRAY_FILTER = $id;
     if (!empty($criteria)) {
         $contents = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'GeoNames' . DIRECTORY_SEPARATOR . $file);
         $pattern = preg_quote($criteria, '/');
         $pattern = "/^.*{$pattern}.*\$/m";
         if (preg_match_all($pattern, $contents, $matches)) {
             $data = implode("\n", $matches[0]);
             $array = GeoNames::csvToArray($data);
         }
     }
     if (!is_null($id)) {
         switch ($criteria) {
             case "ADM2":
                 $result = array_filter($array, function ($k) {
                     return $k->admin1 == GeoNames::$ID_ARRAY_FILTER;
                 });
                 break;
             case "ADM3":
                 $result = array_filter($array, function ($k) {
                     return $k->admin2 == GeoNames::$ID_ARRAY_FILTER;
                 });
                 break;
             default:
                 $result = array_filter($array, function ($k) {
                     return $k->geonameid == GeoNames::$ID_ARRAY_FILTER;
                 });
                 break;
         }
     } else {
         $result = $array;
     }
     if (sizeof($result) > 0) {
         return $result;
     }
 }