getar.php

With deduplication - jkiel, 12/17/2010 09:18 am

Download (961 Bytes)

 
1
<?php
2
$file = "areacodes.txt";
3
$fh = fopen($file, 'w') or die("can't open file");
4
$lcnt = 0;
5
$expr = "/class=\"txtTabla\"><div align=\"center\">(.+)<\/div><\/td>/";
6
set_time_limit(0);
7
$area_codes = array();
8
for ($i = 0; $i <= 2900; $i = $i + 25){
9
10
        $result = file_get_contents("http://www.cnc.gov.ar/infotecnica/numeracion/indicativosinter.asp?offset=$i");
11
        preg_match_all($expr,$result,$matches);
12
        foreach ($matches[1] as $val){
13
                // Store as key in array to deduplicate
14
                $area_codes[$val] = "set";
15
        }
16
        echo "Read ".($i+1)." to " . ($i + count($matches[1])) . "<br>\n";
17
        @ob_flush();
18
        @flush();
19
20
}
21
echo "Writing ".count($area_codes)." area codes to file...";
22
@ob_flush();
23
@flush();
24
$line = '';
25
foreach ($area_codes as $key => $val){
26
        $line .= '"0' . $key . '",';
27
        $lcnt ++;
28
        if($lcnt>10){
29
                $lcnt = 0;
30
                $line .= "\n\t\t";
31
        }
32
        fwrite($fh, $line);
33
        $line = '';
34
}        
35
36
echo "Done.";
37
@ob_flush();
38
@flush();
39
40
fclose($fh);
41
?>
42