PHP Classes

PHP Address Formatter: Format postal address according to country rules

Recommend this page to a friend!
  Info   View files Example   View files View files (12)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 293 This week: 1All time: 7,479 This week: 95Up
Version License PHP version Categories
address-formater 1.0GNU General Publi...5Localization, PHP 5, Geography


This class can format postal address according to country rules.

It takes as parameters an array of elements that define the postal address like recipient name, street, number, building, locality, province, postal code and country.

The class reads template files that define how to format an address for each country. Currently it provides templates for several countries and a default template for other countries.

The class can format the address for a full mailing or waypoint address. The country name may be optionally omitted.

Innovation Award
PHP Programming Innovation award nominee
May 2015
Number 5

Prize: One year server license IP to country, region, city, latitude, longitude, ZIP code, time zone, area code database
When you want to send a letter or a package via postal mail, you need to write the address according to the rules of formatting the address in each country.

This class can format postal addresses according the rules of the country of the address.

Manuel Lemos
Picture of Dave Smith
  Performance   Level  
Name: Dave Smith is available for providing paid consulting. Contact Dave Smith .
Classes: 51 packages by
Country: United States United States
Age: 59
All time rank: 608 in United States United States
Week rank: 24 Up2 in United States United States Up
Innovation award
Innovation award
Nominee: 32x

Winner: 7x




$adf = new addrformat;

We need to set up some nations (countries). For this example we will do it manually,
however you can easily get the data from a database. We need to supply
the abbreviation and the nation name using the class method - addNation
$adf->addNation('US','United States of America');

//or assigning directly to the class property - nation_array
$adf->nation_array['CA'] = 'Canada';
$adf->nation_array['PT'] = 'Portugal';

//sort the class property - nation_array
//we are also setting the US key/value pair at the top of that order

//processing the posted data
if( !empty($_REQUEST['form_posted']) ){
//create an address array by assigning posted data to tags
$addr_array = array(
'recipient' => $_REQUEST['recipient'],
'building' => $_REQUEST['building'],
'addr_num' => $_REQUEST['addr_num'],
'street' => $_REQUEST['street'],
'unit' => $_REQUEST['unit'],
'locality' => $_REQUEST['locality'],
'province' => $_REQUEST['province'],
'province_abbr' => $_REQUEST['province'],
'postcode' => $_REQUEST['postcode'],
'nation' => $adf->nation_array[$_REQUEST['nation_abbr']],
'nation_abbr' => $_REQUEST['nation_abbr']
//process the address array
    //we are also including the nation in the full address
        <title>Address Manager Example</title>
        <form method="post">
                    <td><label for="recipient">Recipient</label><br><em>(John Smith)</em></td>
                    <td><input type="text" name="recipient" value="<?PHP echo ( empty($_REQUEST['recipient']) ) ? '' : $_REQUEST['recipient'];?>"></td>
                    <td><label for="building">Building</label><br><em>(Tower One)</em></td>
                    <td><input type="text" name="building" value="<?PHP echo ( empty($_REQUEST['building']) ) ? '' : $_REQUEST['building'];?>"></td>
                    <td><label for="addr_num">Address Number</label><br><em>(123 1/2)</em></td>
                    <td><label for="street">Street Name</label><br><em>(North Main St.)</em></td>
                    <td><label for="unit">Unit</label><br><em>(Apartment 101)</em></td>
                    <td><input type="text" name="addr_num" value="<?PHP echo ( empty($_REQUEST['addr_num']) ) ? '' : $_REQUEST['addr_num'];?>"></td>
                    <td><input type="text" name="street" value="<?PHP echo ( empty($_REQUEST['street']) ) ? '' : $_REQUEST['street'];?>"></td>
                    <td><input type="text" name="unit" value="<?PHP echo ( empty($_REQUEST['unit']) ) ? '' : $_REQUEST['unit'];?>"></td>
                    <td><label for="locality">Locality/City</label><br><em>(Boston)</em></td>
                    <td><label for="province">Province/State</label><br><em>(MA or Massachusetts)</em></td>
                    <td><label for="postcode">Postcode</label><br><em>(02130)</em></td>
                    <td><input type="text" name="locality" value="<?PHP echo ( empty($_REQUEST['locality']) ) ? '' : $_REQUEST['locality'];?>"></td>
                    <td><input type="text" name="province" value="<?PHP echo ( empty($_REQUEST['province']) ) ? '' : $_REQUEST['province'];?>"></td>
                    <td><input type="text" name="postcode" value="<?PHP echo ( empty($_REQUEST['postcode']) ) ? '' : $_REQUEST['postcode'];?>"></td>
                    <td><label for="nation">Nation/Country</label></td>
                        <select name="nation_abbr">
                            <option value="">Select one...</option>
//get the nation options from the class property nation_array
foreach( $adf->nation_array as $key=>$value ){
$selected = ( !empty($_REQUEST['nation_abbr']) AND $key == $_REQUEST['nation_abbr'] ) ? ' selected' : '';
<option value="<?PHP echo $key;?>"<?PHP echo $selected;?>><?PHP echo $value;?></option>
                        <input type="hidden" name="form_posted" value="1">
                        <input type="submit" name="form_submit" value="Submit"> [<a href="example.php">Start over</a>]
//only show this if the class has built an address array
if( !empty($adf->addr_array) ){
<div><strong>Full Address</strong></div>
        <div style="margin-top:10px;"><?PHP echo nl2br($adf->addr_array['full_addr']);?></div>
        <div style="margin-top:10px;"><strong>Single Line Address</strong><br><em>(aka waypoint which can be used with mapping services)</em></div>
        <div style="margin-top:10px;"><?PHP echo $adf->addr_array['single_line'];?></div>
<div style="margin-top:10px;"><strong>Class Values</strong></div>
        <div><?PHP var_dump($adf);?></div>

  Files folder image Files  
File Role Description
Files folder imageformats (8 files)
Plain text file addrformat.class.php Class Main Class
Accessible without login Plain text file example.php Example Example usage
Accessible without login Plain text file example_cascade.php Example Example usage for Cascading location selects
Accessible without login Plain text file manual.txt Doc. Documentation

  Files folder image Files  /  formats  
File Role Description
  Accessible without login Plain text file default.txt Data Format definition - default
  Accessible without login Plain text file default_1.txt Data Format waypoint definition - default
  Accessible without login Plain text file EN.txt Data Format definition - England
  Accessible without login Plain text file EN_1.txt Data Format waypoint definition - England
  Accessible without login Plain text file PT.txt Data Format definition - Portugal
  Accessible without login Plain text file PT_1.txt Data Format waypoint definition - Portugal
  Accessible without login Plain text file SW.txt Data Format definition - Star Wars
  Accessible without login Plain text file SW_1.txt Data Format waypoint definition - Star Wars

 Version Control Unique User Downloads Download Rankings  
This week:1
All time:7,479
This week:95Up