Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP如何按类从ul列表中拆分li项?_Php_Parsing_Split_Html Lists - Fatal编程技术网

PHP如何按类从ul列表中拆分li项?

PHP如何按类从ul列表中拆分li项?,php,parsing,split,html-lists,Php,Parsing,Split,Html Lists,我试图使用简单的DOM解析器,通过类名解析ul列表中的每个li项。下面您可以看到我试图解析的代码,但请记住它来自html源代码,只是选择了我想要解析的代码 我试过使用这段PHP代码,但根本不起作用 include 'simple_html_dom.php'; $url = "http://www.pinterest.com/avast/"; $html = file_get_html( $url ); $first_level_items = $html->find( '.userSt

我试图使用简单的DOM解析器,通过类名解析ul列表中的每个li项。下面您可以看到我试图解析的代码,但请记住它来自html源代码,只是选择了我想要解析的代码

我试过使用这段PHP代码,但根本不起作用

include 'simple_html_dom.php';

$url = "http://www.pinterest.com/avast/";

$html = file_get_html( $url );
$first_level_items = $html->find( '.userStats', 0)->children();

foreach ( $first_level_items as $item ) {
print_r($first_level_items);
}
这是ul列表,我想从中获取li项目

<ul class="userStats">

<li>
<a href="/avast/boards/" type="button" class="NavigateButton Button Module ButtonBase hasText borderless active BoardCount">
<span class="buttonText">    
  13 opslagstavler    
</span>
</a>
</li>

<li>
<a href="/avast/pins/">
<div class="PinCount Module">
  317 pins    
</div>
</a>
</li>

<li>
<a href="/avast/likes/">
  1 synes om    
</a>
</li>

</ul>

任何帮助都将不胜感激

您考虑过使用xpath查询吗

$xml = new SimpleXMLElement($your_ul_html);
$result = $xml->xpath('//ul[@class="userStats"]/li');
foreach($result as $node) {
    print_r($node); // or echo $node-asXML();
}