-2

It is about my site, it is a ad portal and 3 geodata are installed in the system: Germany, Switzerland and Austria.

When I look for an advertisement in Germany, everything works correctly, I'm looking for zip code 68259 and a radius of 30 km. The results are correct, it shows all ads from 68259 Mannheim and the radius of 30 km.

Problem: The problem exists when I search in Switzerland or Austria: I search for the postal code 6000 Lucerne 1 PF and a radius of 30 km ... the results are wrong, I also find ads from Munich or Frankfurt which correspond to 300-500 km radius! I think the mistake is somewhere in the regex postal verification! Any advice what could be wrong???

// Germany Postcode
    preg_match('/\b((?:0[1-46-9]\d{3})|(?:[1-357-9]\d{4})|(?:[4][0-24-9]\d{3})|(?:[6][013-9]\d{3}))\b/is', $this->search_code, $output);
    if(!empty($output[0])){
        $this->search_code = $output[0];
    }else{
        // Switzerland, Austria Postcode
        preg_match('/\d{4}/', $this->search_code, $at_ch);
        if(!empty($at_ch[0])){
            $this->search_code = $at_ch[0];
        }
    }

   
Biffen
  • 6,249
  • 6
  • 28
  • 36

1 Answers1

0

The following regex will match codes for DE, CH & AU:

'/\b((?:0[1-46-9]\d{3})|(?:[1-357-9]\d{4})|(?:[4][0-24-9]\d{3})|(?:[6][013-9]\d{3})|(?:\d{4}))\b/is'

Examples

68259 Mannheim -> 68259
6000 Lucerne 1 PF -> 6000
1234 Musterstadt -> 1234
John Williams
  • 4,252
  • 2
  • 9
  • 18