0 ? $parts[2] : false); } /* Returns the district for a postcode - for example, SW1A for SW1A 0AA - or * false if the postcode was not in a valid format. The parameter is: * * $postcode - the postcode whose district should be returned */ public static function getDistrict($postcode){ // parse the postcode and return the district $parts = self::parse($postcode); return (count($parts) > 0 ? $parts[1] : false); } /* Returns the sector for a postcode - for example, SW1A 0 for SW1A 0AA - or * false if the postcode was not in a valid format. The parameter is: * * $postcode - the postcode whose sector should be returned */ public static function getSector($postcode){ // parse the postcode and return the sector $parts = self::parse($postcode); return (count($parts) > 0 ? $parts[1] . ' ' . $parts[4] : false); } /* Returns the unit for a postcode - for example, SW1A 0AA for SW1A 0AA - or * false if the postcode was not in a valid format. The parameter is: * * $postcode - the postcode whose unit should be returned */ public static function getUnit($postcode){ // parse the postcode and return the unit $parts = self::parse($postcode); return (count($parts) > 0 ? $parts[1] . ' ' . $parts[3] : false); } /* Returns the post town for a postcode, or the empty string if the post town * could not be determined. The parameter is: * * $postcode - the postcode whose post town should be returned */ public static function getPostTown($postcode){ // determine the district for the postcode $district = self::getDistrict($postcode); // return immediately if the postcode was not in a valid format if (!$district) return false; // check whether the path to the database has been initialised if (!self::$databasePath){ // initialise the path to the database self::$databasePath = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Postcode.data'; // throw an error if the database is missing if (!is_file(self::$databasePath)){ trigger_error( 'Postcode.data not found - ensure you have downloaded the database as well as the PHP class', E_USER_ERROR); } } // open the database $database = fopen(self::$databasePath, 'r'); // initialise the search bounds $start = 0; $end = filesize(self::$databasePath) / self::RECORD_LENGTH - 1; // loop until the bounds have crossed over do{ // determine the index of the midpoint $midpoint = ($start + $end) >> 1; // determine the district for the midpoint fseek($database, $midpoint * self::RECORD_LENGTH); $midpointDistrict = rtrim(fread($database, self::DISTRICT_LENGTH)); // update the bounds or store the post town depending on the district if ($district < $midpointDistrict){ $end = $midpoint - 1; }elseif ($district > $midpointDistrict){ $start = $midpoint + 1; }else{ $postTown = rtrim(fread($database, self::POST_TOWN_LENGTH)); break; } }while ($start <= $end); // close the database fclose($database); // return the post town return (isset($postTown) ? $postTown : ''); } } ?>