2

I have a text-area in which a user can fill info about himself.

I need to detect if user has filled phone no. or email address in that text-area.

I know that a fixed pattern can be matched with regex but how to handle various formats like:

9 7 8 5 8 7 

9, 7, 8, 5, 8, 7 

9.7.8.5.8.7 

Or any 6 or more consecutive number sepereated with \, -, etc

Please help.

Vijay Choudhary
  • 848
  • 1
  • 9
  • 18

3 Answers3

1

I don't think that you'll be able to find all phone numbers and e-mail addresses in a string. It can get very complex. If you don't let people save their profile because you suspicious them to have invalid information, they may try so long until they find a pattern, that your regex won't handle. Someone may end up with:

My name is Peter at googleD0Tcom'on.

On the other hand you'll make people angry because you falsely detect things that aren't a fraud. Let's say you use what Jan Hančič suggested in the comments, to strip all non digit characters (which is not a bad approach):

I'm 21 years old. In 2012 I already had like 45k reputation on Stackoverflow.

This will result in 21201245 and could be part of a phone number.

I would suggest to filter new and updated profiles that look suspicious and to validate them manually. If it's not a valid entry because it contains a disguisedly e-mail or phone number, block it and send an e-mail to the user with the message to update his profile.

I would check the string like:

  • does it contain: @, at or [at], dot, .com (or any other often used TLD by your users)
  • strip all non digit characters and check resulting numbers
  • search for names of huge email companies, like Google, GMX, Yahoo, Apple (ME, iCloud) etc.

Here are some more questions and answers on how to find e-mails and phone numbers sing PHP:

Community
  • 1
  • 1
insertusernamehere
  • 23,204
  • 9
  • 87
  • 126
0

You can use:

\d([,-. ] *\d){5}

It allows , . - and the space character. If you want to include more separator characters, just add them inside the [...]

Oscar Mederos
  • 29,016
  • 22
  • 84
  • 124
0

Try this:

$number    = '12.3555';

if(preg_match('/[\/0-9,-\.\s\\\]{6,}/',$number) && count(array_filter(str_split($number),'is_numeric')) >= 6){
  echo "phone number exists";
}
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73