Jump to content

How would i add brackets to a telephone dialling code


AdRock

Recommended Posts

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.

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;
}
?>

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

?>

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

?>

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);
}

<?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 
*/

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.