-2

I am fetching the file content like

$text = file_get_contents(' MY PAGE URL HERE ');

I want to extract the mail addresses in the page. I am using the below code spinet.

$res = preg_match_all("/[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}/i",$text,$matches);

Normally it is working fine. but getting an issue. If the page contain a mail id like suren.roy@gmail.com then it is extracting only roy@gmail.com. The text before the first . is eliminated.

Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
  • You have to define the part before your dot (".") in your regex as a group so that it is put to your `$matches`. – mario.van.zadel Aug 31 '15 at 10:58
  • possible duplicate of [How to get email address from a long string](http://stackoverflow.com/questions/1028553/how-to-get-email-address-from-a-long-string) – Emil Aug 31 '15 at 10:58

1 Answers1

0

you can try this :

preg_match('/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/si',$text, $matches);
Happy Coding
  • 2,517
  • 1
  • 13
  • 24