简体   繁体   中英

Formatting mobile phone number in PHP

I've been racking my brains on how to format a simple mobile phone number but can't think on how to do it. A friend said to me I could use the preg_replace function but am unsure as I've read up on it but not familiar with it. Basically what I want to do is go through a string say a message that contains: "Hi there my name is John can you call me on 07771234567 thanks" and replace it so it reads "Hi there my name is John can you call me on 07xx12x4xx7 thanks". In other words replace part of the number with an "x".

I'm also aware that some people may write a mobile like this: "0777 123 4567" so would need it to strip any white spaces too. Plus it would need to look for the start and end of the number although I've used a function called stripos() to find the start of the number by looking for the start position of "07".

Any help would be appreciated. Thanks!

Something like this should work, although there are probably direct regex methods:

$foo = '07771234567';
$foo = preg_replace( '/\s+/', ' ', $foo ); // remove whitespaces
$foo = str_split($foo); // split into an array so we can loop over it more easily
$replace_with_x = array(2, 3, 6, 8, 9); // the positions of numbers you want to replace

// loop through your phone number, and replace with x where necessary
for($i=0; $i<len($foo); $i++) {
   if(in_array($i, $replace_with_x)) {
      $foo[$i] = 'x';
   }
}

http://www.braemoor.co.uk/software/telnumbers.shtml

You can download a PHP function from this site that allows you to validate all the different kinds of UK phone numbers.

I am using this is a live system atm so it def works.

Good Luck

This does not account for all formats, Some phone numbers can be placed in different formats and can't just be done via checking for 011, or length. You would need a complete list of all area codes/combination to check against for each possible length.

http://support.hostgator.com/articles/specialized-help/making-international-calls-from-the-united-states For example, you might see a phone number in the United Kingdom (UK) expressed one of these ways:

(+44) (0)20 7930 4832 +44 20 7930 4832 020 7930 4832

Simple solution might be. Also i'm not happy with this code, i'm sure if can be simplified about a thousand times.

private function cleanNumber($uglyNumber){

    $number = preg_replace("/[^0-9]/","",$uglyNumber);
    if(($check = substr($number,0,3))!=='011'){
        //NON 011 Numbers

        switch(strlen($number)){
            //Same Area Code 
            //123-4567
            case 7:
                $prefix = substr($number,0,3);
                $SLN = substr($number,3,10);
                $cleanNumber = $prefix.'-'.$SLN;
                break;

            //Domestic US
            //No International Number
            case 10:
            $area = substr($number,0,3);
            $prefix = substr($number,3,3);
            $SLN = substr($number,6,10);
            $cleanNumber = '('.$area.') ' .$prefix.'-'.$SLN;
                break;

            case 11:
            // International Number [1]
                $country = substr($number,0,1);
                $area = substr($number,0,3);
                $prefix = substr($number,4,3);
                $SLN = substr($number,7,10);
                $cleanNumber = '['.$country .'] ('.$area.') ' .$prefix.'-'.$SLN;
                break;

            case 12:
            // International Number [12]
                $country = substr($number,0,2);
                $area = substr($number,0,3);
                $prefix = substr($number,4,3);
                $SLN = substr($number,7,10);
                $cleanNumber = '['.$country .'] ('.$area.') ' .$prefix.'-'.$SLN;
                break;

            case 13:
            // International Number [123]
                $country = substr($number,0,3);
                $area = substr($number,0,3);
                $prefix = substr($number,4,3);
                $SLN = substr($number,7,10);
                $cleanNumber = '['.$country .'] ('.$area.') ' .$prefix.'-'.$SLN;
                break;

            default:
                //dono 
                return $number;
        }

    }else{
        switch(strlen($number)){

            case 11:
                // ExitUS 001 [1]
                //No Area Code Country
                $exitUs = substr($number,0,3);
                $country = substr($number,3,1);
                $prefix = substr($number,4,3);
                $SLN = substr($number,7,11);
                $cleanNumber = ''.$exitUs .' ['.$country.'] ' .$prefix.'-'.$SLN;
                break;

            case 12:
                // ExitUS 001 [12]
                //No Area Code Country
                $exitUs = substr($number,0,3);
                $country = substr($number,3,2);
                $prefix = substr($number,4,3);
                $SLN = substr($number,7,12);
                $cleanNumber = ''.$exitUs .' ['.$country.'] ' .$prefix.'-'.$SLN;
                break;

            case 13:
                // ExitUS 001 [123]
                //No Area Code Country
                $exitUs = substr($number,0,3);
                $country = substr($number,3,3);
                $prefix = substr($number,6,3);
                $SLN = substr($number,9,13);
                $cleanNumber = ''.$exitUs .' ['.$country.'] ' .$prefix.'-'.$SLN;
                break;

            case 14:
                // ExitUS 001 [1]
                // With Country Code
                $exitUs = substr($number,0,3);
                $country = substr($number,3,1);
                $area   = substr($number,4,3);
                $prefix = substr($number,7,3);
                $SLN = substr($number,10,14);
                $cleanNumber = ''.$exitUs .' ['.$country.'] ('.$area.') ' .$prefix.'-'.$SLN;
                break;

            case 15:
                // ExitUS 001 [12]
                // With Country Code
                $exitUs = substr($number,0,3);
                $country = substr($number,3,2);
                $area   = substr($number,5,3);
                $prefix = substr($number,8,3);
                $SLN = substr($number,11,4);
                $cleanNumber = ''.$exitUs .' ['.$country.'] ('.$area.') ' .$prefix.'-'.$SLN;
                break;

            case 16:
                // ExitUS 001 [123]
                // With Country Code
                $exitUs = substr($number,0,3);
                $country = substr($number,3,3);
                $area   = substr($number,6,3);
                $prefix = substr($number,9,3);
                $SLN = substr($number,12,4);
                $cleanNumber = ''.$exitUs .' ['.$country.'] ('.$area.') ' .$prefix.'-'.$SLN;
                break;

            default:
                //dono 
                return $number;
        }

    }

    return $cleanNumber;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM