PHP Mailing Address Formatter

From ivc wiki
Jump to navigationJump to search

This is a handy PHP function to properly format a mailing label for letters and packages. The get_address_format() method takes an array of all the mailing details and outputs a text string with the right formatting and line structure. Includes most countries.

For example:

Firstname Lastname
99 Street
Apartment 1A
City, AB 54321
Country

Usage:

require_once('class.shipping.php');
$mailing = new Shipping();

// re-arrange elements if needed - $details is an array containing all the mailing information
$addr = array($details['company'], $details['firstname'], $details['lastname'], $details['street1'], $details['street2'],
    $details['city'], $details['zone'], $details['state'], $details['zip'], $details['country'], $details['countrycode']);
// get properly formatted mailing address
$address = trim($mailing->get_address_format($addr));

// show mailing address
echo $address;

Code for class.shipping.php:

<?php

class Shipping {

 public function get_address_format($address) {

$address_format = array(
 "AD" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!city, !postal_code\r\n!country_name_if",
 "AE" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city, !zone_code\r\n!country_name_if",
 "AF" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!city, !zone_code !postal_code\r\n!country_name_if",
 "AG" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!city, !zone_code !postal_code\r\n!country_name_if",
 "AM" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "AR" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!postal_code !zone_name, !city\r\n!country_name_if",
 "AT" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!postal_code !city\r\n!country_name_if",
 "AU" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "AW" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!city !postal_code\r\n!country_name_if",
 "BA" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "BB" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "BE" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!postal_code !city\r\n!country_name_if",
 "BG" => "!first_name !last_name\r\n!company\r\n!street1\r\n!street2"
         ."\r\n!zone_name\r\n!postal_code !city !country_name_if",
 "BH" => "!company\r\n!first_name !last_name\r\n!street1 !street2"
         ."\r\n!zone_name !postal_code\r\n\r\n!country_name_if",
 "BM" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!city - !postal_code\r\n!zone_code\r\n!country_name_if",
 "BR" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!city, !postal_code\r\n!zone_name, !country_name_if",
 "BS" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "BW" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "BZ" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "CA" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!city, !postal_code\r\n!zone_name, !country_name_if",
 "CH" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!postal_code !city\r\n!country_name_if",
 "CL" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"    
         ."\r\n!city, !postal_code\r\n!zone_name, !country_name_if",
 "CN" => "!company\r\n!last_name !first_name\r\n!street1\r\n!street2\r\n"
         ."!city, !zone_name !postal_code\r\n!country_name_if",
 "CO" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "CR" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "CZ" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!postal_code !city\r\n!country_name_if",
 "DE" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!postal_code !city\r\n!country_name_if",
 "DK" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!postal_code !city\r\n!country_name_if",
 "DO" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "DZ" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!city\r\n!postal_code !zone_code\r\n!country_name_if",
 "EC" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "EG" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!city !zone_name !postal_code\r\n!country_name_if",
 "EE" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!postal_code !city\r\n!zone_name\r\n!country_name_if",
 "ES" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!postal_code !city\r\n!country_name_if",
 "FI" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!postal_code !city\r\n!country_name_if",
 "FR" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
    ."\r\n!postal_code !city\r\n!country_name_if",
 "GB" => "!first_name !last_name\r\n!company\r\n!street1\r\n!street2"
        ."\r\n!city\r\n!postal_code\r\n!country_name_if",
 "GE" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "GI" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "GL" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!postal_code !city\r\n!country_name_if",
 "GR" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!postal_code, !city\r\n!country_name_if",
 "GU" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "HK" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!zone_name, !country_name_if",
 "HR" => "!company\r\n!first_name !last_name\r\n!street1 !street2"
         ."\r\n!postal_code !zone_name\r\n!country_name_if",
 "HU" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "ID" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !postal_code \r\n!country_name_if",
 "IE" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city, !zone_name !postal_code\r\n!country_name_if",
 "IL" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !postal_code\r\n!country_name_if",
 "IM" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "IN" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city - !postal_code\r\n!zone_code\r\n!country_name_if",
 "IQ" =>  "!first_name !last_name\r\n!zone_name !zone_code\r\n!street1\r\n!street2"
        ."!city\r\n!postal_code\r\n!country_name_if",
 "IR" =>  "!first_name !last_name\r\n!country_name - !zone_name - !city,"
        ."\r\n!street1\r\n!street2\r\n!postal_code",
 "IS" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!postal_code !city\r\n!country_name_if",
 "IT" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!postal_code, !city\r\n!zone_code\r\n!country_name_if",
 "JM" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city - !postal_code\r\n !country_name_if",
 "JP" => "!postal_code\r\n!zone_name !city\r\n!street1\r\n!street2\r\n"
        ."!company\r\n!last_name !first_name\r\n!country_name_if",
 "KE" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "KH" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "KR" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "KZ" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!postal_code !city\r\n!country_name_if",
 "LC" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "LK" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "LT" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!postal_code !city\r\n!country_name_if",
 "LU" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "LV" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city, !postal_code\r\n!country_name_if",
 "MA" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "MC" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "ME" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "MK" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "MM" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "MO" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "MT" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "MU" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "MY" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!postal_code !city\r\n!zone_name\r\n!country_name_if",
 "MX" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!postal_code !city\r\n!country_name_if",
 "MZ" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city, !zone_name\r\n!country_name_if",
 "NC" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "NI" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "NG" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city - !postal_code\r\n!zone_code\r\n!country_name_if",
 "NL" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!postal_code !city\r\n!country_name_if",
 "NO" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!postal_code !city\r\n!country_name_if",
 "NP" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "NZ" => "!first_name !last_name\r\n!company\r\n!street1\r\n!street2"
        ."\r\n!city !postal_code\r\n!country_name_if",
 "OM" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city\r\n!zone_name, !country_name_if",
 "PA" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "PE" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_name !postal_code\r\n!country_name_if",
 "PH" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city, !zone_name !postal_code\r\n!country_name_if",
 "PK" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city, !zone_code\r\n!country_name_if",
 "PL" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city, !zone_code !postal_code\r\n!country_name_if",
 "PR" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "PT" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city, !zone_name\r\n!country_name_if",
 "RE" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "RO" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"  
        ."\r\n!city, !postal_code\r\n!zone_name, !country_name_if",
 "RS" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "RU" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!postal_code !city\r\n!country_name_if",
 "SA" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city, !postal_code\r\n!country_name_if",
 "SC" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "SE" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!postal_code !city\r\n!country_name_if",
 "SG" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!country_name, !postal_code",
 "SI" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "SK" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "SN" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "SR" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "SV" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
         ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "TH" => "!first_name !last_name\r\n!company\r\n!street1\r\n!street2"
        ."\r\n!city, !zone_name !postal_code\r\n!country_name_if",
 "TN" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "TR" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!postal_code, !city\r\n\r\n!country_name_if",
 "TW" => "!company\r\n!last_name !first_name\r\n!street1\r\n!street2"
        ."\r\n!city, !zone_name !postal_code\r\n!country_name_if",
 "TZ" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "UA" => "!country_name_if\r\n!zone_name\r\n!city\r\n!postal_code\r\n!street1\r\n!street2"
        ."\r\n!company\r\n!first_name !last_name",
 "UK" => "!first_name !last_name\r\n!company\r\n!street1\r\n!street2"
        ."\r\n!city\r\n!postal_code\r\n!country_name_if",
 "US" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city, !zone_code !postal_code\r\n!country_name_if",
 "UY" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "VE" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "VG" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "VI" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city !zone_code !postal_code\r\n!country_name_if",
 "VN" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city, !zone_code !postal_code\r\n!country_name_if",
 "ZA" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city\r\n!postal_code\r\n!country_name_if",
 "ZW" => "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
        ."\r\n!city\r\n!country_name_if"
 );

  // find country format - if not default to generic format
  if (!$format = $address_format[$address[count($address)-1]]) {
    $format = "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
              ."\r\n!city\r\n!zone_code !zone_name\r\n!postal_code\r\n!country_name_if";
  }

  // replace placeholders - array structure must match
  $mailing = str_replace(
   array('!company','!first_name','!last_name','!street1','!street2','!city',
    '!zone_code','!zone_name','!postal_code','!country_name_if'),
   $address,
   $format);

  // remove empty lines in supplied data - fields not populated
  $mailing = preg_replace('/(\r\n[ ]*\r\n)/s', "\r\n", $mailing);

  return $mailing;
 }

}