Location: PHPKode > scripts > getRo > getro/getRo.php
<?php
/**
 * Get all the counties and all the localities from Romania
 * 
 * Author: punctweb.com <mailto:hide@address.com>
 * Version: 0.1
 * Started: 11 Oct 2008 17:13
 * 
 * Released under the terms of Creative Commons Attribution-Share Alike 3.0 licence
 * http://creativecommons.org/licenses/by-sa/3.0/
 */

class getRo {
    /**
     * The page with all the counties in Romania
     */
    const RO_URL = 'http://www.cartiagricole.ro/primarii_judete.php';
    
    /**
     * The page with all the localities in a given county
     * %s - county name
     */
    const COUNTY_URL = 'http://www.cartiagricole.ro/primarii_localitati/%s.html';
    
    /**
     * Match the county name
     */
    const COUNTY_PATTERN = '`<a href="http://www.cartiagricole.ro/primarii_localitati/(.*).html" title="(.*)" class="bold">(.*)</a>`isU';
    
    /**
     * Match the locality name
     */
    const LOCALITY_PATTERN = '`<a href="http://www.cartiagricole.ro/primarie/(.*).html" title="(.*)" style="line-height: 20px;">(.*)</a>`isU';
    
    public function __construct() {
    
    }
    
    /**
     * Fetch the source content
     * @return string
     * @param string $url
     */
    private function connect($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
        curl_setopt($ch, CURLOPT_REFERER, "http://www.google.ro");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        
        $result = curl_exec($ch);
        
        curl_close($ch);
        
        return $result;
    }
    
    /**
     * Get all counties
     * @return array
     */
    public function getCounties() {
        preg_match_all(self::COUNTY_PATTERN, $this->connect(self::RO_URL), $counties, PREG_PATTERN_ORDER);
        
        return $counties[1];
    }
    
    /**
     * Count the availabe counties
     * @return string
     */
    public function countCounties() {
        return count($this->getCounties());
    }
    
    /**
     * Get all localities in a given county
     * @return array
     * @param string $county
     */
    public function getLocalities($county) {
        preg_match_all(self::LOCALITY_PATTERN, $this->connect(sprintf(self::COUNTY_URL, $county)), $localities, PREG_PATTERN_ORDER);
        
        return $localities[2];
    }
    
    /**
     * Count the availabe localities in a given county
     * @return string
     * @param string $county
     */
    public function countLocalities($county) {
        return count($this->getLocalities($county));
    }
}
Return current item: getRo