0

With the help of http://www.regexr.com/ I created this preg_replace:

preg_replace("/\+[\d]{2}|\(0\)|\(|\)|\s/","",$number);

which changes any of these

+27123456789
+27 12 345 6789
(+27)123456789
(+27) 12 345 6789
+27 (0) 12 345 6789
+27(0)123456789
012 345 6789
0123456789

into 0123456789 (South African phone number format)

However, it doesn't change these two and no matter how I set it up, it doesn't want to work:

27 (0) 12 345 6789
27(0)123456789

How can I change the regex match so that it works with those as well?

Bird87 ZA
  • 2,313
  • 8
  • 36
  • 69
  • What should `27 (0) 12 345 6789` become? – anubhava Sep 18 '14 at 13:59
  • Also `0123456789`. This is in a parser to get all the formats the same. – Bird87 ZA Sep 18 '14 at 14:05
  • ok can you show expected output in your question. – anubhava Sep 18 '14 at 14:08
  • `+27123456789` will be changed into `123456789`, not `0123456789`. Does it matter? – sp00m Sep 18 '14 at 14:08
  • Yes it does, it must display a 0 there. If it's not possible I can just do a manual check there. – Bird87 ZA Sep 18 '14 at 14:13
  • Similar question: [Regular expression match phone number with \[a|b\] pattern](http://stackoverflow.com/q/25336913/3622940) – Unihedron Sep 18 '14 at 14:38
  • Try this regex: http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation/20971688#20971688 It's a "bit" overkill, but works for you. It matches the formats you have and many more (including extensions). I believe you can easily change it to your needs. – Ismael Miguel Sep 18 '14 at 14:53

1 Answers1

1

You can do it in a few pass for easier manipulation. Click on example to see the visualization.

  1. \+[\d]{2}|[()] to remove +27, ( and ). Example
  2. [ ]+ to remove all the spaces in between. Example
  3. (^\d{9}(?=$)) replace with 0\1 to add a 0 if it's only 9 digits long.

EDIT1: As @Alan Moore pointed out for 3,we don't need look ahead for $. So (^\d{9}$) will suffice. Example

EDIT2: Since the author wanna match 27(0)..., just add an additional step 4 as below.

  1. 270?(\d{9}) replace with 0\1. Match numbers starting with 27(0) and replace with 0 and the last 9 digits. Example

Note: you need to enable 'g' modifier for the first two and 'gm' modifier for the last one. We need m because we wanna match ^ at the start of each line.

Steven Xu
  • 605
  • 6
  • 12
  • `$` is already a zero-width assertion; you don't need to wrap it in a lookahead. – Alan Moore Sep 18 '14 at 16:04
  • Oh ya. Good catch! It's a mistake because I've been messing around with look ahead/behind for this few days. I'll update it. – Steven Xu Sep 18 '14 at 16:13
  • This doesn't remove just plain 27... it leaves the 27 there. – Bird87 ZA Sep 19 '14 at 05:03
  • The current design is built on the example that you gave. It will match only `+27` altogether. If `+` is not present, it will not match anything. So you wanna match `27123456789` as well? – Steven Xu Sep 19 '14 at 05:11
  • If you wanna match `27(0)...`, just add this as a step 4. `27(?:0?)(\d{9})` replace with `0\1`. See [here](http://regex101.com/r/sJ0zN4/1). – Steven Xu Sep 19 '14 at 05:23
  • You are welcome. Btw `(?:0?)` is redundant and can be replace with `0?` as seen in the answer EDIT2. – Steven Xu Sep 19 '14 at 05:30