Re: How do I extract link text from anchor tag as well as the URL from the "href" attribute

From: Date: Sat, 22 Aug 2009 05:07:46 +0000
Subject: Re: How do I extract link text from anchor tag as well as the URL from the "href" attribute
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-297070@lists.php.net to get a copy of this message
Hello, on 08/16/2009 04:33 AM chrysanhy said the following: > I have the following code to extract the URLs from the anchor tags of an > HTML page: > > $html = new DOMDocument(); > $htmlpage->loadHtmlFile($location); > $xpath = new DOMXPath($htmlpage); > $links = $xpath->query( '//a' ); > foreach ($links as $link) > { $int_url_list[$i++] = $link->getAttribute( 'href' ) . "\n"; } > > If I have a link <a href="http://X.com" >>YYYY</a>, how do I extract the > corresponding YYYY which is displayed to the user as the text of the link > (if it's an image tag, I would like a DOMElement for that). > Thanks You may want to try this HTML parser class that comes with filter class and an example script named test_get_html_links.php that does exactly what you ask. http://www.phpclasses.org/secure-html-filter -- Regards, Manuel Lemos Find and post PHP jobs http://www.phpclasses.org/jobs/ PHP Classes - Free ready to use OOP components written in PHP http://www.phpclasses.org/

« previous php.general (#297070) next »