forked from saaiful/postcode-bd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bdpostcode.php
74 lines (73 loc) · 3.12 KB
/
bdpostcode.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
require_once 'simple_html_dom.php';
function makeCSV($datas)
{
foreach ($datas as $key => $data) {
$x[] = implode(",", $data);
}
return implode("\n", $x);
}
function b2e($str)
{
return str_replace(['০', '১', '২', '৩', '৪', '৫', '৬', '৭', '৮', '৯'], ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], $str);
}
// EN
$html = file_get_contents('https://en.wikipedia.org/wiki/List_of_postal_codes_in_Bangladesh');
$html = str_get_html($html);
$division = '';
$divisions = [];
$datas = $datas1 = [];
foreach ($html->find('.mw-headline') as $key => $value) {
if (preg_match("/Division/", $value->innertext)) {
$division = str_replace(" Division", '', $value->innertext);
$divisions[] = $division;
}
}
foreach ($html->find('.wikitable') as $key => $value) {
if (preg_match("/Thana/", $value->innertext)) {
$division = $divisions[$key];
foreach ($value->find('tr') as $value2) {
if (!preg_match("/SubOffice/", $value2->innertext)) {
$x['division'] = $division;
$x['district'] = strip_tags(@$value2->find('td')[0]->innertext);
$x['thana'] = strip_tags(@$value2->find('td')[1]->innertext);
$x['suboffice'] = strip_tags(@$value2->find('td')[2]->innertext);
$x['postcode'] = strip_tags(@$value2->find('td')[3]->innertext);
$datas[$x['postcode']]['en'] = $x;
$datas1[] = $x;
}
}
}
}
// BN
$html = file_get_contents('https://bn.wikipedia.org/wiki/%E0%A6%AC%E0%A6%BE%E0%A6%82%E0%A6%B2%E0%A6%BE%E0%A6%A6%E0%A7%87%E0%A6%B6%E0%A7%87%E0%A6%B0_%E0%A6%AA%E0%A7%8B%E0%A6%B8%E0%A7%8D%E0%A6%9F_%E0%A6%95%E0%A7%8B%E0%A6%A1%E0%A7%87%E0%A6%B0_%E0%A6%A4%E0%A6%BE%E0%A6%B2%E0%A6%BF%E0%A6%95%E0%A6%BE');
$html = str_get_html($html);
$division = '';
$divisions = [];
$datas2 = [];
foreach ($html->find('.mw-headline') as $key => $value) {
if (preg_match("/বিভাগ/", $value->innertext)) {
$division = str_replace(" বিভাগ", '', $value->innertext);
$divisions[] = $division;
}
}
foreach ($html->find('.wikitable') as $key => $value) {
if (preg_match("/থানা/", $value->innertext)) {
$division = $divisions[$key];
foreach ($value->find('tr') as $value2) {
if (!preg_match("/উপঅফিস/", $value2->innertext)) {
$x['division'] = $division;
$x['district'] = strip_tags(@$value2->find('td')[0]->innertext);
$x['thana'] = strip_tags(@$value2->find('td')[1]->innertext);
$x['suboffice'] = strip_tags(@$value2->find('td')[2]->innertext);
$x['postcode'] = strip_tags(@$value2->find('td')[3]->innertext);
$datas[b2e($x['postcode'])]['bn'] = $x;
$datas2[] = $x;
}
}
}
}
file_put_contents('postcode_en.csv', makeCSV($datas1));
file_put_contents('postcode_bn.csv', makeCSV($datas2));
file_put_contents('postcode.json', json_encode($datas, JSON_UNESCAPED_UNICODE));
file_put_contents('postcode-pretty.json', json_encode($datas, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));