Alex Rivera | Logout

How do I find the last element in an HTML File with PHP Simple HTML DOM Parser?

Asked 2011-01-25T09:10:23.010
10

According to the documentation for SIMPLE HTML DOM PARSER (under the tab “How to modify HTML Elements”), this code finds the first instance of <div class="hello">:

$html = str_get_html('<div class="hello">Hello</div><div class="world">World</div>');

$html->find('div[class=hello]', 0)->innertext = 'foo';

echo $html; // Output: <div class="hello">foo</div><div class="world">World</div>

What if I want to insert 'foo' into the last instance of <div class="hello">, assuming that the HTML code has a lot of instances of <div class="hello">.

What should replace the 0?

Edit
Report

1 Answer

0

lastChild property returns the last child object of an element.

EDIT: not JQuery obviously :) See the W3C selector reference instead: http://www.w3.org/TR/css3-selectors/#last-child-pseudo

answered 2011-01-25T09:24:55.980

Your Answer