Advertisement
stijn1989

Untitled

Apr 25th, 2011
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.67 KB | None | 0 0
  1. <?php
  2. set_time_limit(0);
  3.  
  4. $contents = file_get_contents("http://nl.wikipedia.org/wiki/Lijst_van_gemeenten_in_het_Vlaams_Gewest");
  5. $lines = explode("\n", $contents);
  6. $start = false;
  7. $file = "E:\Projects\WebApps\gemeentes.xml";
  8.  
  9. $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<gemeentes>\n";
  10. $aantal = 0;
  11.  
  12. foreach($lines as $line) {
  13.     if(preg_match("~<a href=\"(/wiki/.+?)\"~", $line, $matches)) {
  14.         if($matches[1] == "/wiki/Aalst_(Oost-Vlaanderen)") {
  15.             $start = true;
  16.         }
  17.        
  18.         if($matches[1] == "/wiki/Categorie:Alles") {
  19.             $start = false;
  20.         }
  21.        
  22.         if($start == true) {
  23.             $c = file_get_contents("http://nl.wikipedia.org" . $matches[1]);
  24.             if(preg_match("~Overige informatie~", $c)) {
  25.                 $start = strpos($c, "<a href=\"/wiki/Postcode\">Postcode</a>");
  26.                 $end = strpos($c, "<a href=\"/wiki/Netnummer\"");
  27.                 $c = substr($c, $start, $end-$start);
  28.                
  29.                 preg_match_all("~(\d{4})~", $c, $results);
  30.                 $c2 = substr($c, strpos($c, "Deelgemeente</a></b><br>"));
  31.                 preg_match_all("~(<a.*>)?([a-zA-Z\-\s]+)(</a>)?(<br( /)?>|</td>)~", $c2, $results2);
  32.                
  33.                 if(count($results[1]) == count($results2[2])) {
  34.                     $count = count($results[1]);
  35.                     $aantal += $count;
  36.                     for($i = 0 ; $i < $count ; $i++) {
  37.                         $xml .= "\t<gemeente>\n";
  38.                         $xml .= "\t\t<postcode>" . $results[1][$i] . "</postcode>\n";
  39.                         $xml .= "\t\t<naam>" . trim($results2[2][$i]) . "</naam>\n";
  40.                         $xml .= "\t</gemeente>\n";
  41.                     }
  42.                 }
  43.             }
  44.         }
  45.     }
  46. }
  47.  
  48. $xml .= "\t<totaal aantal=\"$aantal\" />\n";
  49. $xml .= "</gemeentes>";
  50. //lege lijn weghalen
  51. $xml = preg_replace("~^\n$~", "", $xml);
  52.  
  53. file_put_contents($file, $xml);
  54.  
  55. header('content-type: text/xml');
  56. echo $xml;
  57. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement