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

喜欢而已 提交于 2019-12-13 17:11:42

问题


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.


回答1:


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




回答2:


It's better to do:

$html->find('figure#XXX > *[2]',0)->plaintext;


来源:https://stackoverflow.com/questions/16147044/php-simple-html-dom-parser-call-to-a-member-function-children-on-a-non-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!