-1

i have html like this:

<li>
    TEXT       <---- GET THIS TEXT
    <ul>
      <li>a</li>
      <li>aa</li>
    </ul>
</li>

I want to get "TEXT" in li element, but then i try get li element I get all elements... This is my code:

$html = str_get_html('<li>TEXT<ul><li>a</li><li>aa</li></ul></li>');
echo $html->find('li', 0)->plaintext

output: TEXTaaa

but I need get only TEXT. And I can't add id or or something else

Vykintas
  • 401
  • 1
  • 8
  • 23

3 Answers3

0

Each part before/after a node is a textnode, so you just need to get the first childnode:

$foo->firstChild->textContent;

I'm assuming Simple HTML Dom implements DOMDocument...

Ruben Vincenten
  • 807
  • 4
  • 7
0

I solved it! What you needed was to grab the first textnode:

<?php

require_once 'simple_html_dom.php';

$html = str_get_html('<li>TEXT<ul><li>a</li><li>aa</li></ul></li>');
echo $html->find('li text', 0)->plaintext;

?>
Neil
  • 14,063
  • 3
  • 30
  • 51
  • OK, another example: $html = str_get_html('
  • TEXTb
    • a
    • aa
  • '); echo $html->find('li', 0)->first_child()->plaintext; now I get "b" how get "TEST" in this situation? – Vykintas Mar 14 '17 at 07:14
  • You simply look at the next text node: echo $html->find('li text', 1)->plaintext; – Neil Mar 14 '17 at 07:17