From d682dcc7178b6f2df89f4a2ebccb16efb5a121c9 Mon Sep 17 00:00:00 2001 From: FusionPBX Date: Fri, 8 Oct 2021 16:38:48 -0600 Subject: [PATCH] Update the format_string function --- resources/functions.php | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/resources/functions.php b/resources/functions.php index 989a2cd51d..d164131c0f 100644 --- a/resources/functions.php +++ b/resources/functions.php @@ -724,23 +724,38 @@ function switch_module_is_running($fp, $mod) { //switch_module_is_running('mod_spidermonkey'); //format a number (n) replace with a number (r) remove the number -function format_string ($format, $data) { +function format_string($format, $data) { + //preset values $x=0; $tmp = ''; - for ($i = 0; $i <= strlen($format); $i++) { - $tmp_format = strtolower(substr($format, $i, 1)); - if ($tmp_format == 'x') { - $tmp .= substr($data, $x, 1); - $x++; - } - elseif ($tmp_format == 'r') { - $x++; - } - else { - $tmp .= $tmp_format; + + //count the characters + $format_count = substr_count($format, 'x'); + $format_count = $format_count + substr_count($format, 'R'); + $format_count = $format_count + substr_count($format, 'r'); + + //format the string if it matches + if ($format_count == strlen($data)) { + for ($i = 0; $i <= strlen($format); $i++) { + $tmp_format = strtolower(substr($format, $i, 1)); + if ($tmp_format == 'x') { + $tmp .= substr($data, $x, 1); + $x++; + } + elseif ($tmp_format == 'r') { + $x++; + } + else { + $tmp .= $tmp_format; + } } } - return $tmp; + if (strlen($tmp) == 0) { + return $data; + } + else { + return $tmp; + } } //get the format and use it to format the phone number