1

I am new to HTML DOM parsing with PHP, there is one page which is having different content in its but having same 'class', when I am trying to fetch content I am able to get content of last div, Is it possible that somehow I could get all the content of divs having same class request you to please have a look over my code:

<?php
    include(__DIR__."/simple_html_dom.php");
    $html = file_get_html('http://campaignstudio.in/');
    echo $x = $html->find('h2[class="section-heading"]',1)->outertext; 
?>

1 Answer 1

2

In your example code, you have

echo $x = $html->find('h2[class="section-heading"]',1)->outertext; 

as you are calling find() with a second parameter of 1, this will only return the 1 element. If instead you find all of them - you can do whatever you need with them...

$list = $html->find('h2[class="section-heading"]');
foreach ( $list as $item ) {
    echo $item->outertext . PHP_EOL;
}

The full code I've just tested is...

include(__DIR__."/simple_html_dom.php");
$html = file_get_html('http://campaignstudio.in/');

$list = $html->find('h2[class="section-heading"]');
foreach ( $list as $item ) {
    echo $item->outertext . PHP_EOL;
}

which gives the output...

<h2 class="section-heading text-white">We've got what you need!</h2>
<h2 class="section-heading">At Your Service</h2>
<h2 class="section-heading">Let's Get In Touch!</h2>
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Nigel for your valuable input, but as you can see in source code of this site there are three <h2 class="section-heading"> while adding your code returns two contents of these 3 classes of h2. Request you to help in my mentioned doubt. Thanks in advance
I've just added the full code and the output of the 3 classes.
I don't know sir why at my end its not giving output as you mentioned. outputting as <h2 class="section-heading">At Your Service</h2> <h2 class="section-heading">Let's Get In Touch!</h2> first one is missing somehow
I guess here there is no way to share screen shot else I would have shared you screen shot its still giving on two contents out of three.
Sir what I found is that last two <h2> have only class="section-heading" while the first one is having two class="section-heading text-white" does it is creating any concern, please help with your opinion sir if possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.