Pinning classes or identifiers using PHP Simple HTML DOM Parser

I am trying to select either a class or an identifier using the PHP Simple HTML DOM Parser, completely out of luck. My example is very simple and seems to match the examples given in the manual ( http://simplehtmldom.sourceforge.net/manual.htm ), but it just doesn't work, it forces me up the wall. Other sample scripts defined with a simple dom work fine.

<?php
include_once('simple_html_dom.php');  
$html =  str_get_html('<html><body><div id="foo">Hello</div><div class="bar">Goodbye</div></body></html>');  
$ret = $html->find('.bar')->plaintext;  
echo $ret;  
print_r($ret);  

Can anyone see where I'm wrong?

+3
source share
1 answer

$html->find('.bar'); will return a collection of matching elements, so you need to pass the index as the second parameter:

$ret = $html->find('.bar', 0)->plaintext;

:

foreach($html->find('.bar') as $element) {
    echo $element->plaintext . '<br />';
}
+6

Source: https://habr.com/ru/post/1732990/


All Articles