0

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!

GeordieDave1980
  • 589
  • 4
  • 11
  • 25
  • Why only mobile phone numbers? Is [I18N](http://en.wikipedia.org/wiki/Internationalization_and_localization) relevant for your setting? – nfechner Jul 25 '12 at 09:32
  • http://www.php.net/manual/en/function.substr-replace.php – Quicksilver Jul 25 '12 at 09:34
  • Don't forget mobile numbers can also be written in the form `+447799123456` in the UK (I'm assuming UK since you're going for those starting `07`). Of course, internationally, numbers follow hundreds of conventions – Basic Jul 25 '12 at 09:37

3 Answers3

0

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';
   }
}
Doa
  • 1,965
  • 4
  • 19
  • 35
  • 1
    the str_split is not really necessary, you could just use a second string $bar to which you write either 'x' or the number. – Doa Jul 25 '12 at 09:37
  • Thanks guys! Yes it is on UK numbers and that is a good point so I will make sure I do a search of the message string for either "07" or "+4407" or "+447" for those that type it without the 0. – GeordieDave1980 Jul 25 '12 at 10:00
0

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

mic
  • 1,251
  • 2
  • 15
  • 33
  • Basically what it is i'm trying to do is mask a telephone number that will be left on my message board for my site as yes some people are pretty dumb sometimes and leave their phone numbers for all the world to see so I want it masked so no one else can see it. However as the admin I will be able to see the full thing. The code I used above didn't seem to do the trick, does anyone else know how I could it? Maybe using the way suggested to me - substr_replace() – GeordieDave1980 Jul 25 '12 at 10:28
  • @GeordieDave1980 The trick is to WHEN you do the sanitation - if it's on the way into your data store (database?), it will be stored obfuscated and you won't ever have a clear version (not neccessarily a bad thing is it's not _really_ needed). Alternatively, store it unmodified and apply the filtering suggested in these answers when you render the page. This way the "clear" version is in the database and can be read if it has to be. It's worth noting that none of the methods here are bulletproof (nor are they trying to be), so while this is fine for inadvertent phone numbers, you will... – Basic Jul 25 '12 at 14:10
  • ... need something more robust if you're going to protect yourself against malicious users (eg XSS) – Basic Jul 25 '12 at 14:11
0

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;
}
Jason
  • 51
  • 3