Join GitHub today
Create a new instance. $driver is the driver to be used, currently one of ‘textfile’, ‘mysql’, ‘uk_postcodes’, ‘ukgeocode’, ‘geopostcode’ or ‘openlylocal’. Valid values for $params depend on the driver chosen. See factory(string $driver, array $params)Drivers for more information.
Initialise the connection to the back end. This must always be called before any functions needing to look up postcodes can be called. loaddata()
Sets the value of the separator between outward (SW1A) and inward (1AA) parts of the postcode data. The default depends on the driver. It is a single space usually, but an empty string for uk_postcodes. The use of an empty string is not recommended if your data also includes means or midpoints for districts and sectors, because this can be ambiguous. Eg ‘B11’ could be either the district B11 or the sector B1 1. set_inoutsep(string $sep)
Indicates that the geo data is in wgs84 (latitude and longitude) format. Note that when using the mysql driver if you do not specify the sql parameter in the paramaters passed to factory() and you are using wgs84 you must call use_wgs84() before calling loaddata. This is because the default use_wgs84()SQL is dependent on the co-ordinate scheme being used.
Indicates that the geo data is in osgb36 (British national grid) format.This is the default. Note that the uk_postcodes driver (which uses http://www.uk-postcodes.com) can provide data in either wgs84 or osgb36. The latter makes distance calculations less demanding use_osgb36()
Tests if a postcode is valid and known in the database. Returns the found code on success, false on failure. If the full code is not found, checks will be made for matches by sector (SW1A 1), district (SW1A) and area (SW1). The return value is the best match. validcode(string $postcode [, boolean $exactmatch])
If $exactmatch is true (defaults to false), matches only if the $postcode is known (after normalising $postcode).
Sets the smallest unit that will be checked for by validcode() 1: Area only: SW 2: District: SW1A 3: Sector: SW1A 1 4: Unit: SW1A 1AA set_grain(int $grain)
Returns an array containing the location of the postcode. The elements are (eastings, northings) for osgb36 or (latitude, longitude) for wgs84 getgeodata(string $postcode)
Takes a postcode or partial postcode and attempts to split it into component parts. Returns an array: splitpostcode(string $postcode)
eg, for SW1A 1AA
 => Area,  => District  => Sector  => Unit
Returns false on failure
 => SW  => 1A  => 1  => AA
Checks if $postcode1 is the same postcode as $postcode2. Returns true if the same, false otherwise samepostcode(string $postcode1, string $postcode2)
Returns a normalised version of $postcode, taking account of the current setting of $inoutsep
calculate the distance between $fromcode and $tocode. $options is an optional array: calc_distance (string $fromcode, string $tocode[, array $options])
Like validcode(), if an exact match isn’t available calc_distance will match on sector, district or area. Returns false on failure.
unit => 'miles'|'km' The unit to return the distance in (default miles) decimal => int The number of decimal places (defalt 0)
NB 0 is a valid, good response indicating a distance of 0km or 0miles. Be sure to test for false (eg if($d === false)