Simple html dom get href value

I have an html page that looks a bit like this

xxxx
google!


xxxx

I am trying to pull of the href's out of the big-div. I can get all the href's out of the whole page by using code like below.

$links = $html->find ('a');
foreach ($links as $link) 
{
    echo $link->href.'
'; }

But how do I get only the href's within the div "big-div". Edit: I think I got it. For those that care:

foreach ($html->find('div[class=big-div]') as $element) {
            $links = $element->find('a');
            foreach ($links as $link) {
                echo $link->href.'
'; } }

asked Jan 7, 2014 at 1:12

elgato75elgato75

571 gold badge2 silver badges6 bronze badges

2

The documentation is useful:

$html->find(".big-div")->find('a');

And then proceed to get the href and whatever other attributes you are interested in.

Edit: The above would be the general idea. I've never used Simple HTML DOM, so perhaps you need to tweak the syntax somewhat. Try:

foreach($html->find('.big-div') as $bigDiv) {
   $link = $bigDiv->find('a');
   echo $link->href . '
'; }

or perhaps:

$bigDivs = $html->find('.big-div');

foreach($bigDivs as $div) {
    $link = $div->find('a');
    echo $link->href . '
'; }

answered Jan 7, 2014 at 1:18

Simple html dom get href value

durrruttidurrrutti

1,0101 gold badge8 silver badges17 bronze badges

2

Quick flip - put this in your foreach

$image = $html->find('.big-div')->href;

answered Jan 22, 2019 at 0:23

Simple html dom get href value

DropHitDropHit

1,62515 silver badges27 bronze badges

Get, Set and Remove attributes

// Get a attribute ( If the attribute is non-value attribute (eg. checked, selected...), it will returns true or false)
$value = $e->href;

// Set a attribute(If the attribute is non-value attribute (eg. checked, selected...), set it's value as true or false)
$e->href = 'my link';

// Remove a attribute, set it's value as null!
$e->href = null;

// Determine whether a attribute exist?
if(isset($e->href))
    echo 'href exist!';

Magic attributes

// Example
$html = str_get_html("
foo bar
"); $e = $html->find("div", 0); echo $e->tag; // Returns: " div" echo $e->outertext; // Returns: "
foo bar
" echo $e->innertext; // Returns: " foo bar" echo $e->plaintext; // Returns: " foo bar"
Attribute nameDescription
$e->tag Read or write the tag name of element.
$e->outertext Read or write the outer HTML text of element.
$e->innertext Read or write the inner HTML text of element.
$e->plaintext Read or write the plain text of element.

Tips

// Extract contents from HTML
echo $html->plaintext;

// Wrap a element
$e->outertext = '
' . $e->outertext . '
'; // Remove a element, set it's outertext as an empty string $e->outertext = ''; // Append a element $e->outertext = $e->outertext . '
foo
'; // Insert a element $e->outertext = '
foo
' . $e->outertext;