Question

I would like to get this code:

<div class="class1">Any Text<div>Or any other Content</div></div>

to this:

<div class="class1 another_class"><div class="extra"></div>Any Text<div>Or any other Content</div></div>

with the PHP Simple HTML Dom Parser

This is my first part:

foreach($html->find(".class1") as $element) {
    $element->class="class1 another_class";
}

But how can I add the "extra"-div?

Was it helpful?

Solution

($e->innertext)Read or write the inner HTML text of element. may be this gonna work.. :)

foreach($html->find("div.class1") as $element) {
    $element->innertext = '<div class="extra"></div>' . $element->innertext;
    $element->class = "class1 another_class";
}

OTHER TIPS

The correct syntax is this;

$ret = $html->find('div.foo');
//OR
$ret = $html->find('div[class=foo]');

source: http://simplehtmldom.sourceforge.net/manual.htm
How to find HTML elements? section, tab Advanced

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top