使用simple-html-dom-parser从$ element返回单个项目 [英] Return single item from $element with simple-html-dom-parser

查看:71
本文介绍了使用simple-html-dom-parser从$ element返回单个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

simple-html-dom-parser的新手,有一个问题.

New to the simple-html-dom-parser and have a question.

假设$element是一个数组,如何从数组中返回特定项目(在这种情况下为图像).我试过$element[1]$element->childNodes ([1])无济于事.

Assuming $element is an array, how can I return a specific item (an image in this case) from the array. I have tried $element[1] and $element->childNodes ([1]) to no avail.

$html = file_get_html($url);

foreach($html->find('img') as $element)
    {
    echo $element;
    }

Simple-html-dom-parser api

推荐答案

根据您发布的文档,find使用第二个可选参数,它是您要查找的元素的索引.因此,例如,您可以执行$html->find('img', 1)并且它将返回它可以找到的第一个img元素.

According to the documentation you posted, find takes a second optional argument which is the index of the element you're looking for. So, for example, you could do $html->find('img', 1) and it would return the first img element it could find.

希望这会有所帮助.

这篇关于使用simple-html-dom-parser从$ element返回单个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆