1

I have source code :

<li class="parent">
<a href="#">F</a>
<ul>
    <li>xx</li>
    <li>xx</li>
     ......
</ul>
</li>

I want get plaintext of tag li:nth-child(1) by simple_html_dom :

$html->find("li.parent li:nth-child(1)")

not

$html->find("li.parent li",0);
10
  • 1
    to find the text of the a tag you have to use this line of code $html->find("li.parent a") Commented Jun 2, 2015 at 7:30
  • Oh, sorry... :D i want find li:nth-child(1) Commented Jun 2, 2015 at 7:33
  • I still don't know what is your problem/question. Commented Jun 2, 2015 at 7:33
  • @LuthandoLoot if you have only one a tag else you must loop over the result ;) Commented Jun 2, 2015 at 7:33
  • @Maraboc yes you are right Commented Jun 2, 2015 at 7:35

2 Answers 2

2

You can't use complicated css with simple html dom. I suggest switching to this one:

$html = <<<EOF
<li class="parent">
<a href="#">F</a>
<ul>
    <li>xxx</li>
    <li>yyy</li>
</ul>
</li>
EOF;

require_once('advanced_html_dom.php');
$doc = str_get_html($html);
echo $doc->find("li.parent li:nth-child(2)", 0)->text;
// yyy
Sign up to request clarification or add additional context in comments.

2 Comments

It not work :( Catchable fatal error: Argument 2 passed to DOMXPath::query() must be an instance of DOMNode, null given, called in D:\Work\PHP\xampp\htdocs\crawler\includes\base\advanced_html_dom.php on line 89 and defined in D:\Work\PHP\xampp\htdocs\crawler\includes\base\advanced_html_dom.php on line 50
That means it's not there. See my edit, this works fine as is.
0

To find only the first child of the ul you can use:

$html->find("li.parent li:nth-child(1)")

OR

$html->find("li.parent li:first-child")  

But to get all the li's you have to use a loop like this:

 foreach($html->find("li.parent li") as $element) 
   echo $element->plaintext. '<br>';

12 Comments

If i want get li.parent li:nth-child(2,3...4...n) ? Because i have file config. My code read this file. In file have structure selector css. I want change them in file config, not edit my code. Ex: $html->find("li.parent li",0...n) by $html->find("li.parent li:nth-child(1...n)")
you can use the first option to get li in any position you want, e.g if you want the second li you can get it this way $html->find("li.parent li:nth-child(2)")
Thanks ! It seems it does not working. I have a multi-level menu, I want to take just under the menu tag a in the parent menu , and not get a in the submenu. e.g : $html->find("li.parent > a") .. But it get all tag a in parent menu and submenu .. LOL !
i dont understand what you're saying, if you want the sub li's text you cannot use a tag in your selector because those li's are not under the a tag
try this foreach($html->find("li.parent > a > ul > li:nth-child(1) > a")) echo $element->href. '<br>';
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.