Welcome Guest, Not a member yet? Register   Sign In
Format Phone Numbers (Addition to string helper)
#1

[eluser]Iverson[/eluser]
In case anybody needs this...(US Phone Numbers)

Code:
function phone_format($str)
{
   // Keep only be digits
   $strPhone = ereg_replace("[^0-9]",'', $str);
   if(strlen($strPhone) != 10)
   {
      return $strPhone;
   }

   $strArea = substr($strPhone, 0, 3);
   $strPrefix = substr($strPhone, 3, 3);
   $strNumber = substr($strPhone, 6, 4);

   $strPhone = '(' . $strArea . ') ' . $strPrefix . '-' . $strNumber;

   return $strPhone;
}
#2

[eluser]tdktank59[/eluser]
Modified it to take any digits after 10 and make them the extension

MY_string_helper.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Formats phone numbers
* If phone number is longer than 10 digits
* The rest are considered an extension
*
* @param varchar $str The unformated phone number
* @param boolean $extension TRUE/FALSE to use the extension
* @return Formated Phone number
*/
function phone_format($str,$extension = FALSE)
{
    // Keep only be digits
    $strPhone = ereg_replace("[^0-9]",'', $str);

    $strArea = substr($strPhone, 0, 3);
    $strPrefix = substr($strPhone, 3, 3);
    $strNumber = substr($strPhone, 6, 4);
    $strExtens = substr($strPhone,10);

    $strPhone = '(' . $strArea . ') ' . $strPrefix . '-' . $strNumber;

    if ($strExtens && $extension)
    {
        $strPhone .= ' #'.$strExtens;
    }

    return $strPhone;
}
#3

[eluser]ray73864[/eluser]
what is the resulting format from this? i can probably guess, but then US numbers are formatted differently to Australian phone numbers, very rarely these days do we put the area code inside of brackets for instance, and mobile numbers changes the whole thing too.

a lot of our phone numbers have characters in them too (since certain businesses like being fancy like that).
#4

[eluser]tdktank59[/eluser]
[quote author="ray73864" date="1230691146"]what is the resulting format from this? i can probably guess, but then US numbers are formatted differently to Australian phone numbers, very rarely these days do we put the area code inside of brackets for instance, and mobile numbers changes the whole thing too.

a lot of our phone numbers have characters in them too (since certain businesses like being fancy like that).[/quote]


Well. Basically just change $strPhone to what ever format you would like
If you would like to show me an example of an australian phone number ill be
More than happy to modify it for you!

At the moment the format is

(xxx) xxx-xxxx #xxxx

(Area) prefix - number #extension
#5

[eluser]Milos Dakic[/eluser]
Here is how most numbers are formated

home phones (00) 0000-0000
mobiles 000 000 000 0

The fancy numbers that ray73864 was talking about, are things like:

13 POND (13 76 63)

1300 NUMBER (1300 50 10 50)
#6

[eluser]ray73864[/eluser]
over here in Western Australia, we format phone numbers as (we ignore the brackets and dashes completely):

Home/Fax: 00 0000 0000
Mobile: 0000 000 000




Theme © iAndrew 2016 - Forum software by © MyBB