2

I'm trying to remove / detect phone numbers from messages between users of my marketplace website (think eBay does something similar) this is the code I'm using:

$string = preg_replace('/([0-9]+[\- ]?[0-9]+)/', '', $string);

BUT... it's too aggressive and it does strip away any number with 2 or more numerals... how can set a limit of say 7 numbers instead?

to be more precise the phone numbers can be any format like

3747657654
374-7657654
374-765-7654
(374)765-7654
etc...(i cannot predict what the users will write depending of their habits)
4
  • 1
    Without some idea of the string you are trying to detect phone number in we cannot be a lot of help. Add a sample(s) to your question Commented Feb 20, 2016 at 17:13
  • any phone number in the world written by a human: either a sequence of 7 or more numbers, in all possible way a phone is formatted in the western countries. i can't predict what the users will write Commented Feb 20, 2016 at 17:38
  • 1
    i cannot predict what the users will write depending of their habits How will you figure out difference between 7 digit phone# and 7 digit money e.g. 1234567? Commented Feb 21, 2016 at 9:07
  • Before you can write code to detect phone numbers, you have to explain in English how you define a phone number. Giving examples isn't an English definition. Commented Nov 29, 2017 at 15:50

4 Answers 4

1

Try this regular expression :

/([0-9]+[\- ]?[0-9]{6,})/

changed to match your samples: Regex101

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

1 Comment

yes it does BUT also ti does remove any number like a time stamp eg. "hey I will see you there from 4pm to 8 my cell is 3747657654"
0

That would depend on the exact requirements as now you have 1 or more numbers followed by an optional - or space followed by 1 or more numbers again.

If you wanted for example at least 2 numbers before the space or - followed by at least 5 numbers, you could use something like:

$string = preg_replace('/([0-9]{2,}[\- ]?[0-9]{5,})/', '', $string);
                                              ^^^^ Here you can specify mininimum / maximum
                               ^^^^ Here you can specify mininimum / maximum

Comments

0

You can try something like this:

$string = preg_replace('/(?<![0-9]|[0-9]-)[0-9](?:[- ]?[0-9]){6}(?!-?[0-9])/', '', $string);

The lookarounds are here to avoid numbers with more than 7 digits, but if you want something more specific, you should provide an example string.

Comments

0

It is impossible to determine whether a number of X digits (where X is a valid phone number length) is a phone number or something else without some sort of context intelligence happening. A simple regex can't determine the difference between "call me at 3453456" and "call me when you've flown 3453456 miles".

Therefore trying to catch phone numbers without any formatting (just straight digits) with a regex is hopeless, pure and simple. Attempting to do so is only holding you back from finding a regex that can find formatted/semi-formatted numbers. What you should be going for here is "get the obvious and as many others as possible with minimal false positives...but recognize I can't get them all."

For that I'd recommend this:

/1?[ \-]?\(?([0-9]{3})?\)?[ \-]?([0-9]{3})[ \-]([0-9]{4})/g

It should not get the first three, but get all the rest in this list:

no-match: 3747657654
no-match: 444444444444444
no-match: 7657654
match:    374-765-7654
match:    1-374-765-7654
match:    (374)765-7654
match:    (374) 765 7654
match:    765-7654
match:    1 (374) 765 7654
match:    1(374)765 7654

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.