Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you wouldn't suprress your error reporting, you would have seen that you have a fatal error that breaks your script.</p> <p>In order to work with the <strong>item</strong> method:</p> <pre><code>foreach ($lis as $li) { if (method_exists($li-&gt;childNodes, 'item')) { echo $li-&gt;childNodes-&gt;item(0)-&gt;nodeValue."&lt;br /&gt;"; // To reproduce the exact output you need this line also. // You need to display the second child (Answer) echo $li-&gt;childNodes-&gt;item(1)-&gt;nodeValue."&lt;br /&gt;"; } } </code></pre> <p>The only difference it was that the first script</p> <pre><code>foreach ($lis as $li) { echo $li-&gt;firstChild-&gt;nodeValue."&lt;br /&gt;"; echo $li-&gt;lastChild-&gt;nodeValue."&lt;br /&gt;"; //echo $li-&gt;childNodes-&gt;item(0)-&gt;nodeValue."&lt;br /&gt;"; } </code></pre> <p>Only throws <strong>Notice: Trying to get property of non-object</strong>, but the scripts continues.</p> <p>As with method item() it throws a fatal error. <strong>(Fatal error: Call to a member function item() on a non-object).</strong> which kills your script.</p> <p>For more details on how you should iterate on these nodesList (foreach vs. for) read the comments from these pages</p> <ul> <li><a href="http://www.php.net/manual/en/class.domnodelist.php" rel="nofollow">http://www.php.net/manual/en/class.domnodelist.php</a></li> <li><a href="http://www.php.net/manual/en/domnodelist.item.php" rel="nofollow">http://www.php.net/manual/en/domnodelist.item.php</a></li> </ul> <p>And you especially have this issue because of the trailing space after the <code>&lt;li&gt;</code> tags.</p> <p>It loops like this: first <code>&lt;li&gt;</code> tag, then the space <code>' ' DOMText</code> element then the second <code>&lt;li&gt;</code> tag then the second <code>' ' DOMText</code> element. </p> <p>On the DOMText element it crashes. You could clear the spaces and it would work.</p> <pre><code>$html_data = '&lt;html&gt;&lt;body&gt;&lt;ol&gt;&lt;li&gt;&lt;strong&gt;Question 1&lt;/strong&gt; Answer1&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Question 2&lt;/strong&gt; Answer2&lt;/li&gt;&lt;/ol&gt;&lt;/body&gt;&lt;/html&gt;'; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload