AdRock Posted March 21, 2008 Share Posted March 21, 2008 On my form I allow user to enter a telephone number such as 01654 635483 How would i get the brackets around the dialling code? Link to comment https://forums.phpfreaks.com/topic/97278-how-would-i-add-brackets-to-a-telephone-dialling-code/ Share on other sites More sharing options...
Jeremysr Posted March 21, 2008 Share Posted March 21, 2008 First get rid of all characters except numbers (just in case they put spaces, hyphens, brackets, etc. as seperators): $phone_number = preg_replace('/[^0-9]/', '', $phone_number); Then you might want to check if it's valid (just check the length of the string to see how many digits there are.) Then seperate it into parts using substr(): $first_part = substr($phone_number, 0, 3); $second_part = substr($phone_number, 3, 3); $third_part = substr($phone_number, 6, 4); That would take a phone number like 3659469245 and seperate it like this: 365, 946, 9245. Finally, concatenate them back together but with brackets: $phone_number = "($first_part) $second_part-$third_part"; I don't know what phone number format you want exactly though. Link to comment https://forums.phpfreaks.com/topic/97278-how-would-i-add-brackets-to-a-telephone-dialling-code/#findComment-497807 Share on other sites More sharing options...
cooldude832 Posted March 21, 2008 Share Posted March 21, 2008 I wrote this a while back <?php function phone_format($number){ $number = preg_replace("[^0-9]","",$number); $len = strlen($number); if($len == 11){ $output = substr($number,0,1)."(".substr($number,1,3).")".substr($number,4,3).".".substr($number,7,4); } elseif($len == 10){ $output ="(". substr($number,0,3).")".substr($number,3,3).".".substr($number,6,4); } elseif($len == 7){ $output = substr($num,0,3).".".substr($num,4,3); } elseif($len == 0){ $output = "None"; } else{ $output = $number; } return $output; } ?> Link to comment https://forums.phpfreaks.com/topic/97278-how-would-i-add-brackets-to-a-telephone-dialling-code/#findComment-497808 Share on other sites More sharing options...
Barand Posted March 21, 2008 Share Posted March 21, 2008 here's one I wrote earlier <?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 echo '<br />'; echo format_template ('07123456789', '+44 (#)### ### ####'); // --> +44 (0)712 345 6789 echo '<br />'; echo format_template ('ab123456x', '!! ## ## ## !'); // --> AB 12 34 56 X ?> Link to comment https://forums.phpfreaks.com/topic/97278-how-would-i-add-brackets-to-a-telephone-dialling-code/#findComment-497813 Share on other sites More sharing options...
AdRock Posted March 21, 2008 Author Share Posted March 21, 2008 Thanks everyone for the qucil repsonses I tried using this but i'm not getting the brackets in the right places I am trying to put brackets around the UK dialling code which is always 5 numbers. The rest of the string, i don't care about as long as the first 5 digits have brackets around them I am getting output like this 0(184)5 6.43543 instead of (01845)643543 also, the last part of the phone number can be 5 digits in length here's one I wrote earlier <?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 echo '<br />'; echo format_template ('07123456789', '+44 (#)### ### ####'); // --> +44 (0)712 345 6789 echo '<br />'; echo format_template ('ab123456x', '!! ## ## ## !'); // --> AB 12 34 56 X ?> Link to comment https://forums.phpfreaks.com/topic/97278-how-would-i-add-brackets-to-a-telephone-dialling-code/#findComment-497904 Share on other sites More sharing options...
Jeremysr Posted March 21, 2008 Share Posted March 21, 2008 Well then try this: $phone_number = preg_replace('/[^0-9]/', '', $phone_number); if (strlen($phone_number) < 6) { // It's invalid } else { $phone_number = '(' . substr($phone_number, 0, 5) . ')' . substr($phone_number, 5); } Link to comment https://forums.phpfreaks.com/topic/97278-how-would-i-add-brackets-to-a-telephone-dialling-code/#findComment-497910 Share on other sites More sharing options...
cooldude832 Posted March 21, 2008 Share Posted March 21, 2008 Mine only works on US numbers I could expand it except I have no use it returns 1(555)555.5555 (555)555.5555 555.5555 Link to comment https://forums.phpfreaks.com/topic/97278-how-would-i-add-brackets-to-a-telephone-dialling-code/#findComment-497917 Share on other sites More sharing options...
Barand Posted March 21, 2008 Share Posted March 21, 2008 <?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('01845643543', '(#####)######') /** * although UK formats differ * MAJOR CITY (eg MANCHESTER) (0161) 123 4567 * TOWNS (eg REDDITCH) (01527) 12345 */ ?> Link to comment https://forums.phpfreaks.com/topic/97278-how-would-i-add-brackets-to-a-telephone-dialling-code/#findComment-497919 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.