vsergiu
vsergiu

Reputation: 137

phonenumber regular expression to fix the prefix

Basically what I need to do is when I have a phonenumber like this one: 31655334868 I need to change that to: 1655334868 (removed the first 3)

But I can also have 0031655334868 and then I need to change that to: 1655334868.

How can i do this in PHP with a regex? Or is there a better alternative way?

Upvotes: 0

Views: 456

Answers (2)

w00
w00

Reputation: 26762

If you want to do it with regex then you could use the following code:

(this also takes into account that you could have a number starting with +31)

$phone1 = '31655334868';
$phone2 = '0031655334868';
$phone3 = '+31655334868';

if ( preg_match ( '/^.*3([0-9]{10})$/', $phone2, $matches ) )
{
    print_r($matches);
    echo $matches[1]; // will always contain the desired result
}
else
{
    echo 'no';
}

Working example:

http://codepad.viper-7.com/FAuh2c

Upvotes: 1

Nick
Nick

Reputation: 639

echo substr($phoneNumber, -10);

Upvotes: 0

Related Questions