Tags
PHP
Asked 3 years ago
1 Jul 2021
Views 554
Rickie

Rickie posted

get all a href attribute list of given url in php

get all a href attribute list of given url in php
sec8

sec8
answered Oct 8 '21 00:00


I use mostly PHP HTML DOM Parser for getting html dom elment and values or text etc..
see following code how to use PHP HTML DOM Parser for getting all a href attribute list of given url.

$html = file_get_html('http://www.google.com/');
$ret = $html->find('a');
foreach($ret as $element){
if(isset($element->href))
        echo 'href is '.$element->href;
}


1. get html of the given url by file_get_html() function.

$html = file_get_html('http://www.google.com/');


2. find <a> element by find function as you do in jQuery.

$ret = $html->find('a');

$html->find('a') will find all <a> element and return as object array

3. iterate through all objects of <a> element. and print href value as per the above code.

Post Answer