1

Ok so after having re-read over my previous post asking for help on this one I found I wasn't making it exactly clear what I was trying to do nor was I pointing out why. I have a site that I'm busy developing which may possibly display messages where people have left their phone numbers (even though this is stupid) and I need to be responsible and make sure should this be the case the numbers are masked. Firstly I need to search through the message text which is stored in a variable $messagetext then I need to use the preg_replace() function to mask parts of the number so its not clear what the number is so if someone were to leave a message and their number was "07921234567" it would appear on the message as "07**12*45**". How would this be done ? All I would like is to find out what function I'd use to search for the entire number (United Kingdom number) which may start +44 or 07 and what REGEX in the preg_replace() function as all i had was:

$extractednum = preg_replace( "/[0-9]/","*",$extractednum);
echo ($extractednum);

All this does is replace the entire number. The reason why I don't wanna do this is I also have another site I'm working on to do with social networking privacy and I need to mask parts of the telephone numbers I retrieve for my example.

Hopefully this is more clear and if someone could help me out with some code that would be great!

Anything is appreciated!

3
  • 1
    And if the user mangles their number into Zero792One234Five67? Commented Jul 25, 2012 at 14:14
  • 1
    @MarcB: I think he is trying to be as responsible as he can to mask the numbers, but if the user tries to bypass the filter then probably no can do. Commented Jul 25, 2012 at 14:16
  • 1
    Why not just replace the last 9 digits with *s? Then it will always be 07********* or +447********* or 447*********. Commented Jul 25, 2012 at 14:17

3 Answers 3

2

I think the regex you are looking for is this:

(00447|\+?447|07)([0-9]{9})

To mask the phone numbers, you'll need a custom callback with preg_replace_callback(), like this:

$extractednum = preg_replace_callback( "/(00447|\+?447|07)([0-9]{9})/", function( $matches) {
    // This will return the number unmodified
    // return $matches[1] . $matches[2]; 
    // Instead, set whichever characters you want to be "*" like this:
    $matches[2][0] = $matches[2][1] = $matches[2][4] = $matches[2][7] = $matches[2][8] = "*";
    return $matches[1] . $matches[2]; 
} , $extractednum);

You can see it working in the demo. For example, an input of 07921234567 yields 07**12*45** as output.

Sign up to request clarification or add additional context in comments.

2 Comments

I would write that first group as (00447|\+?447|07) to take all possible legitimate expressions of UK mobile numbers into account
@Dave - Thanks for the tip - I added your modified regex into the code. I also created a new demo.
0

I'm not sure how many numbers there are in a valid UK telephone number. Let's assume there are 11 numbers:

preg_replace(/(\d{2})\d{2}(\d{2})\d(\d{2})\d{2}/, "$1**$2*$3**");

This is not exactly a good solution, since I'm guessing that there can be phone numbers with different length, and people can put spaces in the number for visual purpose.

Comments

0
<?PHP
function maskTelephoneNumber($phonenumber, $trim, $maskCharacter)
{
$suffixNumber = substr($phonenumber, strlen($phonenumber)-$trim,$trim); 
$prefixNumber = substr($phonenumber, 0, -$trim); 

for ($x = 0; $x < strlen($prefixNumber); $x++):
    $str.= ( is_numeric($prefixNumber[$x]) )? str_replace($prefixNumber[$x], $maskCharacter, $prefixNumber[$x]) : $prefixNumber[$x];
endfor;

return  $str.$suffixNumber;     
}

$trim = 4;
$maskCharacter='*';
$phonenumber = '555-666-7777';

echo maskTelephoneNumber($phonenumber,$trim, $maskCharacter);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.