International Short & Long Address Formatting


/ Published in: PHP
Save to your folder(s)

I'm not well versed in international address formatting, but this seems to work for most cases. Critiques are welcome!


Copy this code and paste it in your HTML
  1. if($key == 'short_address') {
  2. // this is tricky b/c of international addresses
  3. // if the location is US based then we want to display an identifier similiar to: Bob's Place in Downingtown OH
  4. // if the location is non-USA based (and either city or providence is not available) then we want to display: Bob's Place in Downingtown USA
  5.  
  6. return $this->name.' in '.$this->city.' '.$this->providence.
  7. (empty($this->city) || empty($this->providence) ? ' '.$this->country : '');
  8. } else if($key == 'full_address') {
  9. // target format for US based addresses:
  10. // 300 Road Street
  11. // Town PA, 19335
  12. // target format for non-US based addresses
  13. // address1
  14. // City, Providence Postal Code Country
  15.  
  16. // the trims handle edge cases when there is not enough information to propertly display an address
  17.  
  18. if($this->country == 'US') {
  19. return trim(trim($this->address1."\n".$this->address2)."\n".$this->city).", ".$this->providence." ".$this->postal_code;
  20. } else {
  21. return trim(trim($this->address1."\n".$this->address2)."\n".$this->city).", ".$this->providence." ".$this->postal_code." ".$this->country;
  22. }
  23. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.