How do I find the last <div class> in an HTML File with PHP Simple HTML DOM Parser? -
according documentation simple html dom parser (under tab “how modify html elements”), code finds 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 want insert 'foo' last instance of <div class="hello">
, assuming html code has lot of instances of <div class="hello">
.
what should replace 0
?
well, since
// find anchors, returns array of element objects $ret = $html->find('whatever');
returns array
holding <whatever>
elements, can fetch last element php's regular array functions, e.g. end
$last = end($ret);
if simplehtmldom implements css3 selectors querying, can modify query use
:last-of-type
to find last sibling in returned nodelist.
Comments
Post a Comment