Mostafa Elkady
Mostafa Elkady

Reputation: 5791

Grab e-mail addresses from an HTML table

I have an HTML table that contains 500 e-mail addresses in this file.html. Now I want to open it with PHP and get all e-mail addresses in it and add it to an array. This means I have an HTML table and in this table 500 rows with an e-mail address in each row.

How can I get these e-mail addresses with PHP?

Upvotes: 0

Views: 256

Answers (4)

Brian Agnew
Brian Agnew

Reputation: 272257

I would avoid regexps for all the HTML/regexp issues and use an HTML parser such as this one. It should be trivial to extract the DOM for the table and contained cells/content.

Note that a regexp for identifying emails can potentially be quite complex, depending on the variety of the emails you wish to match. SO has numerous examples.

Upvotes: 1

AntonioCS
AntonioCS

Reputation: 8496

I agree with The MYYN's approach but I suggest you use phps DOM class to iterate through all the td's of the table and fetch the emails.

Here are some useful functions:

loadHTMLFile

and

getElementsByTagName

Upvotes: 1

Luca Matteis
Luca Matteis

Reputation: 29267

You could use regex to match the entire HTML string with email occurrences.

http://www.regular-expressions.info/email.html

Upvotes: 0

miku
miku

Reputation: 188014

You can parse the HTML via php to access the relevant pieces and store them in your data structure for further processing, a short introduction on PHP html parsing can be found here: http://www.onderstekop.nl/articles/114/

Upvotes: 1

Related Questions