1 | <?php |
||
2 | /** |
||
3 | * @link https://en.wikipedia.org/wiki/Category:Postal_system Postal Systems by Country |
||
4 | * @link http://dmoztools.net/Reference/Directories/Address_and_Phone_Numbers/Postal_Codes/ DMOZ Post/Zip Code Info+DB |
||
5 | * @link https://en.wikipedia.org/wiki/List_of_postal_codes List of Postal Codes |
||
6 | * @param $what |
||
7 | * @param $codes |
||
8 | * @return array |
||
9 | * @internal param mixed $ |
||
10 | */ |
||
11 | function get_codes_from($what, $codes, $iso) |
||
12 | { |
||
13 | $what = explode(',', $what); |
||
14 | foreach ($what as $eachArea) { |
||
15 | if (trim($eachArea) != '' && trim($eachArea) != '- no codes -') { |
||
16 | $codes[] = "'".str_replace(['N', 'A', 'CC'], ['#', '@', $iso], trim($eachArea))."'"; |
||
17 | } |
||
18 | } |
||
19 | return $codes; |
||
20 | } |
||
21 | |||
22 | require __DIR__.'/../../../../include/functions.inc.php'; |
||
23 | function_requirements('getcurlpage'); |
||
24 | $page = getcurlpage('https://en.wikipedia.org/wiki/Special:Export/List_of_postal_codes'); |
||
25 | $page = str_replace(["\n\n", '<', '>', '&', '<br />'], ["\n", '<', '>', '&', ''], $page); |
||
26 | $lines = explode("\n", $page); |
||
27 | $found = []; |
||
28 | $out = []; |
||
29 | for ($x = 0, $xMax = sizeof($lines); $x < $xMax; $x++) { |
||
30 | $line = $lines[$x]; |
||
31 | if ((trim($line) == '|-' || trim($line) == '|-.') && mb_substr($lines[$x + 1], 0, 1) != '!') { |
||
32 | $x++; |
||
33 | $country = preg_replace('/\| *\[\[Postal codes in [^\|]*\|(.*)\]\]/msU', '\1', $lines[$x]); |
||
34 | $x++; |
||
35 | $years = trim($lines[$x]); |
||
36 | $x++; |
||
37 | $iso = preg_replace('/\| *\[\[ISO 3166-[0-9]*:[A-Z]*\|(.*)\]\]/msU', '\1', $lines[$x]); |
||
38 | $area = ''; |
||
39 | $street = ''; |
||
40 | $notes = array_key_exists($iso, $out) ? $out[$iso]['notes'] : ''; |
||
41 | if (trim($lines[$x + 1]) != '|-') { |
||
42 | $x++; |
||
43 | $area = trim(mb_substr($lines[$x], 1)); |
||
44 | if (trim($lines[$x + 1]) != '|-') { |
||
45 | $x++; |
||
46 | $street = trim(mb_substr($lines[$x], 1)); |
||
47 | if (trim($lines[$x + 1]) != '|-') { |
||
48 | $x++; |
||
49 | if (trim(mb_substr($lines[$x], 1)) != '') { |
||
50 | $notes .= ($years != '' ? $years.' ' : '') . trim(mb_substr($lines[$x], 1)); |
||
51 | } |
||
52 | } |
||
53 | } |
||
54 | } |
||
55 | $codes = array_key_exists($iso, $out) ? $out[$iso]['codes'] : []; |
||
56 | $codes = get_codes_from($area, $codes, $iso); |
||
57 | $codes = get_codes_from($street, $codes, $iso); |
||
58 | $found[] = $iso; |
||
59 | $out[$iso] = [ |
||
60 | 'codes' => $codes, |
||
61 | 'country' => $country, |
||
62 | 'notes' => $notes, |
||
63 | ]; |
||
64 | } |
||
65 | } |
||
66 | $db = $GLOBALS['tf']->db; |
||
67 | $db->query('select * from country_t order by iso2;'); |
||
68 | while ($db->next_record(MYSQL_ASSOC)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
69 | if (!in_array($db->Record['iso2'], $found)) { |
||
70 | $out[$db->Record['iso2']] = [ |
||
71 | 'codes' => [], |
||
72 | 'country' => $db->Record['short_name'], |
||
73 | 'notes' => '', |
||
74 | ]; |
||
75 | } |
||
76 | } |
||
77 | $keys = array_keys($out); |
||
78 | sort($keys); |
||
79 | foreach ($keys as $iso) { |
||
80 | $codes = $out[$iso]['codes']; |
||
81 | $country = $out[$iso]['country']; |
||
82 | $notes = $out[$iso]['notes']; |
||
83 | echo " '{$iso}' => [".implode(', ', $codes).'],'." // $country".(trim($notes) != '' ? ', Notes: '.$notes : '').PHP_EOL; |
||
84 | } |
||
85 |