Add formatAddress function and replace all addresses with it

This commit is contained in:
johnnyq
2026-07-23 18:10:39 -04:00
parent 9c65644adc
commit 41b118ab87
17 changed files with 60 additions and 40 deletions

View File

@@ -192,6 +192,26 @@ function formatPhoneNumber($phoneNumber, $country_code = '', $show_country_code
}
function formatAddress($address, $city, $state, $zip, $country = '', $separator = "\n") {
$address = trim((string)$address);
$city = trim((string)$city);
$state = trim((string)$state);
$zip = trim((string)$zip);
$country = trim((string)$country);
// "City, ST 15212" - the comma only appears when something follows the city
$region = trim(implode(' ', array_filter([$state, $zip], 'strlen')));
if ($city !== '' && $region !== '') {
$region = "$city, $region";
} elseif ($city !== '') {
$region = $city;
}
// Empty parts drop out entirely rather than leaving stray separators
return implode($separator, array_filter([$address, $region, $country], 'strlen'));
}
function timeAgo($datetime) {
if (is_null($datetime)) {
return "-";
@@ -382,3 +402,9 @@ function secondsToTime($inputSeconds) {
return implode(', ', $timeParts);
}
function formatAddress($address, $city, $state, $zip, $country = '', $separator = "\n") {
$cityLine = trim(implode(' ', array_filter([trim("$city,", ' ,') ? "$city," : '', $state, $zip])));
$cityLine = rtrim($cityLine, ',');
return implode($separator, array_filter([trim($address), $cityLine, trim($country)]));
}