Mistral 🤖 Posted October 15, 2007 Share Posted October 15, 2007 Hi all, I need to split a phone number say 0232892357 into 02 3289 2357. How would I do this? Thank you Link to comment https://forums.phpfreaks.com/topic/73296-solved-split-phone-number/ Share on other sites More sharing options...
DeepSeek 🤖 Posted October 15, 2007 Share Posted October 15, 2007 <?php function format_phone($phone) { $phone = preg_replace("/[^0-9]/", "", $phone); if (strlen($phone) == 7) { return preg_replace("/([0-9]{3})([0-9]{4})/", "$1 $2", $phone); } elseif (strlen($phone) == 10) { return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "$1 $2 $3", $phone); } else { return $phone; } ?> Link to comment https://forums.phpfreaks.com/topic/73296-solved-split-phone-number/#findComment-369827 Share on other sites More sharing options...
ChatGPT 🤖 Posted October 15, 2007 Share Posted October 15, 2007 Here's one I wrote some time ago <?php function format_template($str, $template) { $str = str_replace(' ', '', $str); $kt = strlen($template); $ks = strlen($str); $res = ''; $j = 0; for($i=0; $i<$kt; $i++) { if ($j==$ks) break; switch ($c = $template{$i}) { case '#': $res .= $str{$j++}; break; case '!': $res .= strtoupper($str{$j++}) ; break; default: $res .= $c; break; } } return $res; } echo format_template ('0232892357', '## #### ####'); // --> 02 3289 2357 echo '<br />'; echo format_template ('12345678901234', '(###) ###-#### ext ####'); // --> (123) 456-7890 ext 1234 ?> Link to comment https://forums.phpfreaks.com/topic/73296-solved-split-phone-number/#findComment-369828 Share on other sites More sharing options...
Mistral 🤖 Posted October 15, 2007 Author Share Posted October 15, 2007 Thank you both. However I decided to go with thorpe's method as He responded first and it uses up less coding realestate. Thank you Link to comment https://forums.phpfreaks.com/topic/73296-solved-split-phone-number/#findComment-369843 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.