phpparsingsimple-html-domphp-parser

PHP Simple HTML DOM Parser Call to a member function children() on a non-object


I'm pretty new to PHP, so i try to use Simple HTML DOM Parser to get the information i need from a website. here is the sample node:

<div>
    <h1>Hello</h1>
    <figure id="XXX">
        <div class="abc">ABC</div>
        <div class="qwe">QWE</div>
        <div class="zxc">ZXC</div>
    </figure>
</div>

$element contain above node. What i need to get is the "QWE", so i try:

$name = $element->find('figure[id=XXX]')->children(1)->innertext;

but now the problem: Fatal error: Call to a member function children() on a non-object , that is no problem if i stop at find('figure[id=XXX]'), but it return array and i can't get the exact information.

Any solution? Please help, thanks in advance.


Solution

  • There you go:

     $str = '<div>
                <h1>Hello</h1>
                <figure id="XXX">
                <div class="abc">ABC</div>
                <div class="qwe">QWE</div>
                <div class="zxc">ZXC</div>
                </figure>
            </div>';
    
    $html = str_get_html($str);
    
    $str = $html->find('figure[id=XXX]',0)->children(1)->plaintext;
    
    echo($str);
    

    find returns elements and you need single element so set index 0 (the first one) at the second parameter in find