I want to get <a> tags, how?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wren9
Forum Newbie
Posts: 9
Joined: Sat May 23, 2009 1:50 am

I want to get <a> tags, how?

Post by wren9 »

Good morning guys,

I want to get all <a> tags on a html page.

and including all characters between <a> & </a>

Any codes demo in REGEX or DOM please?

Sorry this is my first time to scrape a page.

Thank you VERY VERY VERY much your codes will be very very helpful to me, since this is my first and currently learning to scrape a html page.

GOD BLESS.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: I want to get <a> tags, how?

Post by requinix »

Could use some DOM class (eg, DOMDocument) then do a search by tag name.

Then there's always regular expressions. Might be better, hard to say.

Code: Select all

preg_match_all('#<a\s.*?</a>#is', $text, $matches);
print_r($matches[0]);
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: I want to get <a> tags, how?

Post by prometheuzz »

tasairis wrote:Could use some DOM class (eg, DOMDocument) then do a search by tag name.

Then there's always regular expressions. Might be better, hard to say.

Code: Select all

preg_match_all('#<a\s.*?</a>#is', $text, $matches);
print_r($matches[0]);
Parsing (x)html should be done with an html parser. When running into improperly formed html, the regex might cause the entire html file to be incorrectly parsed while a true html parser can (an probably will) recover from those mistakes.
Post Reply