1

This is a sample text/string with a mix of numbers and phone numbers:

oh my god i have 1000 things to do today and i want to finish reading 20,000 Leagues Under the Sea, 1954 book, but hey, I will see you there from 4pm to 8 my cell is 374-7657654 or you can call me at (374)-334-5674 and then i can come from 16:30 thru 19:45 (in european time!)

how do i remove/detect ONLY phone numbers (written in any format, since i can't foresee how a user would write them?

i tried

(\(?+[0-9]{3,15}+[\- ]?+[0-9]?+\)?+)

but it strips away the time or book title/ year etc

Francesco
  • 24,839
  • 29
  • 105
  • 152

2 Answers2

4

Here's the regex:

1?\W*([2-9][0-8][0-9])\W*([2-9][0-9]{2})\W*([0-9]{4})(\se?x?t?(\d*))?

preg_match("1?\W*([2-9][0-8][0-9])\W*([2-9][0-9]{2})\W*([0-9]{4})(\se?x?t?(\d*))?", "", $text);

Where $text is the text you wish to validate.

All credit to indiv, answering this question

Community
  • 1
  • 1
IeuanG
  • 504
  • 5
  • 13
0

While you already have an answer, why not come up with something easier:

~([-+()\d]{5,})~
# looks for -+() and digits 
# which need to be there at least 5 consecutive times

See a demo here on regex101.com.

Jan
  • 42,290
  • 8
  • 54
  • 79