Code solutions to PHP, JS, CSSCode source PHP, JS, CSS

Spread your code soluces to the world

Love me
Solucior » PHP » Extract links from a page

Extract links from a page

06/08/2011 by Dux   -   3.7/5 (3 ratings)

Getting all A HREF links from a local or remote page that returns an array.
This function uses the DOM API from PHP 5.

get_a( file ) = array( att, value )

source code

// PHP 5 required

function get_a($file) {
// verify file exists
if (!@fopen($file, "r")) return false;
// Create DOM Document
$ddoc = new DOMDocument();
$tags = $ddoc->getElementsByTagName('a');
   foreach (
$tags as $tag) {
// Get all href attribute
$att = $tag->getAttribute('href');
// Convert node value into html entities
$value = htmlentities(utf8_decode($tag->nodeValue));
// If not empty, add to return tab
if (!empty($att) && !empty($value)) $return[] = array( 'att' =>  $att,'value' =>  $value);
   return (
is_array($return)) ? $return : false;

// Example of use
if ($tab = get_a('')) {
$tab as $display) echo '<a href="/'.$display['att'].'">'.$display['value']."</a><br />\n";
} else {
'no link at this address');
Rate this code     |     Copied : 67x

Comments for Extract links from a page

No comment
Add a comment Nickname   Site

Extract links from a page
Extract images from a page
Page to top
Horizontal scroll div with javascript
Link confirmation
Dynamic image resizing according to location
Dynamically add rows to a form. Complete solution
Scroll div with javascript
Div following the scrolling of the page
Create virtual folders using htaccess