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) { |
12
|
|
|
$what = explode(',', $what); |
13
|
|
|
foreach ($what as $eachArea) |
14
|
|
|
if (trim($eachArea) != '' && trim($eachArea) != '- no codes -') |
15
|
|
|
$codes[] = "'".trim($eachArea)."'"; |
16
|
|
|
return $codes; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
require __DIR__.'/../../../include/functions.inc.php'; |
20
|
|
|
function_requirements('getcurlpage'); |
|
|
|
|
21
|
|
|
$page = getcurlpage('https://en.wikipedia.org/wiki/Special:Export/List_of_postal_codes'); |
|
|
|
|
22
|
|
|
$page = str_replace("\n\n", "\n", $page); |
23
|
|
|
$lines = explode("\n", $page); |
24
|
|
|
$found = []; |
25
|
|
|
for ($x = 0, $xMax = sizeof($lines); $x < $xMax; $x++) { |
26
|
|
|
$line = $lines[$x]; |
27
|
|
|
if ((trim($line) == '|-' || trim($line) == '|-.') && mb_substr($lines[$x + 1], 0, 1) != '!') { |
28
|
|
|
$x++; |
29
|
|
|
$country = preg_replace('/\| *\[\[Postal codes in [^\|]*\|(.*)\]\]/msU', '\1', $lines[$x]); |
30
|
|
|
$x++; |
31
|
|
|
$x++; |
32
|
|
|
$iso = preg_replace('/\| *\[\[ISO 3166-[0-9]*:[A-Z]*\|(.*)\]\]/msU', '\1', $lines[$x]); |
33
|
|
|
$x++; |
34
|
|
|
$area = trim(mb_substr($lines[$x], 1)); |
35
|
|
|
$x++; |
36
|
|
|
$street = trim(mb_substr($lines[$x], 1)); |
37
|
|
|
$x++; |
38
|
|
|
$notes = trim(mb_substr($lines[$x], 1)); |
39
|
|
|
$codes = get_codes_from($area, []); |
40
|
|
|
$codes = get_codes_from($street, []); |
41
|
|
|
$found[] = $iso; |
42
|
|
|
echo " '{$iso}' => [".str_replace(['N', 'A'], ['#', '@'], implode(', ', $codes)).'],'.(count($codes) == 0 ? ' ' : '')." // $country".(trim($notes) != '' ? ', Notes: '.$notes : '').PHP_EOL; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
$db = $GLOBALS['tf']->db; |
46
|
|
|
$db->query('select * from country_t order by iso2;'); |
47
|
|
|
while ($db->next_record(MYSQL_ASSOC)) { |
|
|
|
|
48
|
|
|
if (!in_array($db->Record['iso2'], $found)) |
49
|
|
|
echo " '{$db->Record['iso2']}' => [], // {$db->Record['short_name']}\n"; |
50
|
|
|
} |
51
|
|
|
exit; |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
|
55
|
|
|
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); |
56
|
|
|
//print_r($matches); |
57
|
|
|
foreach ($matches['country'] as $idx => $country) { |
58
|
|
|
$iso = $matches['country_iso'][$idx]; |
59
|
|
|
$area = $matches['area'][$idx]; |
60
|
|
|
$street = $matches['street'][$idx]; |
61
|
|
|
$notes = $matches['notes'][$idx]; |
62
|
|
|
$codes = get_codes_from($area, []); |
63
|
|
|
$codes = get_codes_from($street, $codes); |
64
|
|
|
echo " '{$iso}' => [".implode(', ', $codes)."] // $country".(trim($notes) != '' ? ', Notes: '.$notes : '').PHP_EOL; |
65
|
|
|
} |
66
|
|
|
//$page = getcurlpage('https://en.wikipedia.org/wiki/List_of_postal_codes'); |
67
|
|
|
//function_requirements('xml2array'); |
68
|
|
|
//$data = xml2array($page, 1, 'attribute'); |
69
|
|
|
//$data = xml2array($page, 1); |
70
|
|
|
//print_r($data); |
71
|
|
|
exit; |
72
|
|
|
|
73
|
|
|
/* |
74
|
|
|
$file = file_get_contents('zip.txt'); |
75
|
|
|
$lines = explode("\n", $file); |
76
|
|
|
$zips = []; |
77
|
|
|
foreach ($lines as $line) { |
78
|
|
|
list($country, $street_zip, $street_zip, $notes) = explode("\t", $line); |
79
|
|
|
if (!isset($zips($country))) { |
80
|
|
|
$zips[$country] = []; |
81
|
|
|
$parts = explode(',', $area_zip); |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
} else { |
85
|
|
|
echo "Country $country has area zip of $area_zip and Street zip $street zip With Notes: $notes\n"; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
*/ |
89
|
|
|
|
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.