3

I need to remove all the special characters in a phone number. for ex my phone numbers look like this +91-5345534534, 040-545453453

Finally, I need all the ph numbers should be in unique format like 915345534534, 040545453453

I need a regular expression for this to do in php.

I am trying like this

$ph_number = "+91-5345534534";
filterNum = filterNum.replaceAll("[^0-9]+", $ph_number);

but, this is not working. Please help me in that.

2
  • 2
    Seems more like to be js code than php.
    – Rikesh
    Feb 24, 2014 at 11:08
  • 3
    You do realise that by implementing that you have no way of distinguishing international numbers from national ones anymore? The leading + is part of the phone number.
    – Joey
    Feb 24, 2014 at 11:17

2 Answers 2

12

You are mixing up languages. The first line of code is indeed PHP, but the second is JS?

Although you are looking for the function preg_replace

$ph_number = preg_replace("/[^0-9]/", "", $ph_number);
5

output before-> +91-5345534534

$phone = preg_replace('/[^\dxX]/', '', $ph_number);
echo $phone;

output-> after-> 915345534534

2
  • Why xX in the character class?
    – Toto
    Feb 24, 2014 at 11:52
  • it's nothing just remove it don't be affect that. i used because i have string like this +91-92x89XX786
    – dev4092
    Feb 24, 2014 at 11:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.