geepzbarbz geepzbarbz - 7 months ago 8
PHP Question

how to extract raw html code using simplehtmldom

I am trying to extract raw html from a web-page using simplehtmldom. I was wondering if it is possible using that library.

For example, let's say I have this web page I am trying to extract data from.

<div class="class1">
<div class="class2">
<div class="class3">
<p>p1</p>
<h1>header here!</h1>
<p>p2</p>
<img src="someimage"></img>
</div>
</div>
</div>


My goal is to extract everything within div class3 including the raw html code so when I get the data I can enter it to a text box which allows input for source code so it is formatted the same way it is from the webpage.

I have looked at simplehtmldom manuals and did some searching but have yet to find a solution.

Thank you.

Answer

Using your example html string

 $html = str_get_html('<div class="class1">
  <div class="class2">
    <div class="class3">
    <p>p1</p>
    <h1>header here!</h1>
    <p>p2</p>
    <img src="someimage"></img>
</div>
</div>
</div>');      
// Find all divs with class3
foreach($html->find('div[class=class3]') as $element) {
    echo $element->outertext;
}
Comments