View Full Version : Preg_match_all and Regex for emails

11-25-2010, 02:24 AM
I've got a text file on my computer that is basically a huge amount of emails crammed together with paragraphs of text about each one.

I thought, shoot, I should preg_match_all this since i only need my emails and so I tried but my regex skills are noob level. This code below just shoots out empty arrays: Array ( [0] => Array ( ) [1] => Array ( ) ) . Anyone help?

$email_regex = "/(\w+?@\w+?\x2E.+)/";
$text_file = "C:\Program Files\Email_Files\1.txt"
$html_page = file_get_contents($text_file);

preg_match_all($email_regex, $html_page, $new_emails);
foreach($new_emails as $value)
echo print_r($value) . ", ";

11-25-2010, 02:35 AM
I think there's something wrong with your regexp, but the first thing I would do is be sure to view source on your output page. I suspect that since your input has HTML in it, it's not getting parsed the way you think it is, and if you're viewing the output of print_r() in a browser, the HTML is probably invisible to you.


11-25-2010, 03:03 AM
Hmm, I checked the source and it does look like an HTML page. I wouldn't think that the HTML tags would effect it?

<a href="">example@jimk.com</a>
<p>Jim S. social media group</p>
sarahmjk@yahoo.com designs websites
<p>list: tyuhjj@yahoo jakemonster@gmail.com example@test.com yougetThe@idea.com</p>