1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* @link https://en.wikipedia.org/wiki/Category:Postal_system Postal Systems by Country |
5
|
|
|
* @link http://dmoztools.net/Reference/Directories/Address_and_Phone_Numbers/Postal_Codes/ DMOZ Post/Zip Code Info+DB |
6
|
|
|
* @link https://en.wikipedia.org/wiki/List_of_postal_codes List of Postal Codes |
7
|
|
|
* |
8
|
|
|
* @var mixed |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
$zip_names = [ |
12
|
|
|
'CA' => ['name' => 'Postal Code'], |
13
|
|
|
'english speaking' => ['name' => 'Postcode'], |
14
|
|
|
'NL' => ['name' => 'Postcode'], |
15
|
|
|
'IE' => ['name' => 'Eircode'], |
16
|
|
|
'IT' => ['name' => 'CAP', 'acronym_text' => 'Codice di Avviamento Postale (Postal Expedition Code)'], |
17
|
|
|
'BR' => ['name' => 'CEP', 'acronym_text' => 'Código de endereçamento postal (Postal Addressing Code)'], |
18
|
|
|
'CH' => ['name' => 'NPA', 'acronym_text' => "numéro postal d'acheminement in French-speaking Switzerland and numéro postal d'acheminement in Italian-speaking Switzerland"], |
19
|
|
|
'IN' => ['name' => 'PIN code', 'acronym_text' => 'postal index number.'], |
20
|
|
|
'DE' => ['name' => 'PLZ', 'acronym_text' => 'Postleitzahl (Postal Routing Number)'], |
21
|
|
|
'US' => ['name' => 'ZIP code', 'acronym_text' => 'Zone Improvement Plan'], |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
function get_codes_from($what, $codes) { |
25
|
|
|
$what = explode(',', $what); |
26
|
|
|
foreach ($what as $each_area) |
27
|
|
|
if (trim($each_area) != '' && trim($each_area) != '- no codes -') |
28
|
|
|
$codes[] = "'".trim($each_area)."'"; |
29
|
|
|
return $codes; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
require(__DIR__.'/../../../include/functions.inc.php'); |
33
|
|
|
function_requirements('getcurlpage'); |
34
|
|
|
$page = getcurlpage('https://en.wikipedia.org/wiki/Special:Export/List_of_postal_codes'); |
35
|
|
|
$page = str_replace("\n\n", "\n", $page); |
36
|
|
|
$lines = explode("\n", $page); |
37
|
|
|
$found = []; |
38
|
|
|
for ($x = 0; $x < sizeof($lines); $x++) { |
39
|
|
|
$line = $lines[$x]; |
40
|
|
|
if ((trim($line) == '|-' || trim($line) == '|-.') && mb_substr($lines[$x + 1], 0, 1) != '!') { |
41
|
|
|
$x++; |
42
|
|
|
$country = preg_replace('/\| *\[\[Postal codes in [^\|]*\|(.*)\]\]/msU', '\1', $lines[$x]); |
43
|
|
|
$x++; |
44
|
|
|
$x++; |
45
|
|
|
$iso = preg_replace('/\| *\[\[ISO 3166-[0-9]*:[A-Z]*\|(.*)\]\]/msU', '\1', $lines[$x]); |
46
|
|
|
$x++; |
47
|
|
|
$area = trim(mb_substr($lines[$x], 1)); |
48
|
|
|
$x++; |
49
|
|
|
$street = trim(mb_substr($lines[$x], 1)); |
50
|
|
|
$x++; |
51
|
|
|
$notes = trim(mb_substr($lines[$x], 1)); |
52
|
|
|
$codes = get_codes_from($area, []); |
53
|
|
|
$codes = get_codes_from($street, []); |
54
|
|
|
$found[] = $iso; |
55
|
|
|
echo " '$iso' => [".str_replace(['N', 'A'], ['#', '@'], implode(", ", $codes))."],".(sizeof($codes) == 0 ? ' ' : '')." // $country".(trim($notes) != '' ? ', Notes: '.$notes : '')."\n"; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
$db = $GLOBALS['tf']->db; |
59
|
|
|
$db->query("select * from country_t order by iso2;"); |
60
|
|
|
while ($db->next_record(MYSQL_ASSOC)) { |
61
|
|
|
if (!in_array($db->Record['iso2'], $found)) |
62
|
|
|
echo " '{$db->Record['iso2']}' => [], // {$db->Record['short_name']}\n"; |
63
|
|
|
} |
64
|
|
|
exit; |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
|
68
|
|
|
preg_match_all('/^\|-\.*.^\| \[\[Postal codes in [^\|]*\|(?P<country>[^$]*)\]\]$.^\| *(?P<since>[^$]*)$.^\| *\[\[ISO 3166-1:[A-Z]*\|(?P<iso>[^$]*)\]\]$.^\| *(?P<area>[^$]*)$.^\| *(?P<street>[^$]*)$.^\| (?P<notes>[^$]*)$/msU', $page, $matches); |
69
|
|
|
//print_r($matches); |
70
|
|
|
foreach ($matches['country'] as $idx => $country) { |
71
|
|
|
$iso = $matches['country_iso'][$idx]; |
72
|
|
|
$area = $matches['area'][$idx]; |
73
|
|
|
$street = $matches['street'][$idx]; |
74
|
|
|
$notes = $matches['notes'][$idx]; |
75
|
|
|
$codes = get_codes_from($area, []); |
76
|
|
|
$codes = get_codes_from($street, $codes); |
77
|
|
|
echo " '$iso' => [".implode(", ", $codes)."] // $country".(trim($notes) != '' ? ', Notes: '.$notes : '')."\n"; |
78
|
|
|
} |
79
|
|
|
//$page = getcurlpage('https://en.wikipedia.org/wiki/List_of_postal_codes'); |
80
|
|
|
//function_requirements('xml2array'); |
81
|
|
|
//$data = xml2array($page, 1, 'attribute'); |
82
|
|
|
//$data = xml2array($page, 1); |
83
|
|
|
//print_r($data); |
84
|
|
|
exit; |
85
|
|
|
|
86
|
|
|
/* |
87
|
|
|
$file = file_get_contents('zip.txt'); |
88
|
|
|
$lines = explode("\n", $file); |
89
|
|
|
$zips = []; |
90
|
|
|
foreach ($lines as $line) { |
91
|
|
|
list($country, $street_zip, $street_zip, $notes) = explode("\t", $line); |
92
|
|
|
if (!isset($zips($country))) { |
93
|
|
|
$zips[$country] = []; |
94
|
|
|
$parts = explode(',', $area_zip); |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
} else { |
98
|
|
|
echo "Country $country has area zip of $area_zip and Street zip $street zip With Notes: $notes\n"; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
*/ |
102
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.