Last active
September 20, 2017 23:30
-
-
Save wpscholar/1659054 to your computer and use it in GitHub Desktop.
Format an address
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Takes an array containing address elements and outputs a formatted address. | |
*/ | |
function get_formatted_address( $address, $args = array() ) { | |
$before = isset( $args['before'] ) ? $args['before']: ''; | |
$after = isset( $args['after'] ) ? $args['after']: ''; | |
$format = ( isset( $args['format'] ) && 'block' == $args['format'] ) ? 'block': 'inline'; | |
$formatted_address = ''; | |
foreach( $address as $key => $value ) { | |
if( $value ){ | |
switch( $key ) { | |
//case 'postOfficeBoxNumber': | |
case 'streetAddress': | |
case 'addressLocality': | |
case 'addressRegion': | |
case 'postalCode': | |
//case 'addressCountry': | |
break; | |
default: | |
unset( $address[$key] ); | |
} | |
if( isset( $address[$key] ) ) { | |
$address[$key] = '<span class="' . $key . '">' . $value . '</span>'; | |
} | |
} else { | |
unset( $address[$key] ); | |
} | |
} | |
if( 'block' == $format ) { | |
if( !empty( $address['streetAddress'] ) ) { | |
$formatted_address .= $address['streetAddress'] . '<br />'; | |
unset( $address['streetAddress'] ); | |
$format = 'inline'; | |
} | |
} | |
if( 'inline' == $format ) { | |
$postalCode = ' ' . $address['postalCode']; | |
unset( $address['postalCode'] ); | |
$formatted_address .= join( ', ', $address ) . $postalCode; | |
} | |
if( !empty( $formatted_address ) ) { | |
$formatted_address = $before . '<span class="address">' . $formatted_address . '</span>' . $after; | |
} | |
return $formatted_address; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment