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

6
  • 1
    What makes a number a phone number? The written in any format makes this almost impossible.. Commented Feb 20, 2016 at 17:59
  • i tried many solutions but it strips away the small digits Commented Feb 20, 2016 at 17:59
  • @chris85 is right. The written in any format makes this almost impossible.. Commented Feb 20, 2016 at 18:01
  • @chris85 any number greater than 9 (at least in western countries) that can include characters like () - – Commented Feb 20, 2016 at 18:01
  • In your current example make the last number class not optional. [0-9]+.. Commented Feb 20, 2016 at 18:04

2 Answers 2

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

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

4 Comments

Did you just have that regex lying around?
I found the regex on a separate answer, and put it in preg_match.
Let me double check that regex @Francesco
I was looking for some thing similar vote up from me too, nice work
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.

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.