简体   繁体   中英

PHP Simple HTML Dom: Prepend

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?

($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";
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM