简体   繁体   English

php:php简单的dom解析器问题

[英]php: php simple dom parser question

i'm using the php simple dom parser and found a little issue: 我正在使用php简单的dom解析器,并发现了一个小问题:

when the selector returns only one element, i also need to run a foreach loop for it. 当选择器只返回一个元素时,我还需要为它运行一个foreach循环。 is there a simpler way to do it? 有更简单的方法吗?

thanks 谢谢

The manual says find() has a second parameter for exactly that: 手册说 find()有一个第二个参数:

mixed find ( string $selector [, int $index] ) mixed find(string $ selector [,int $ index])

Find elements by the CSS selector. 通过CSS选择器查找元素。 Returns the Nth element object if index is set, otherwise return an array of object. 如果设置了index,则返回第N个元素对象,否则返回一个object数组。

So in your case, 所以在你的情况下,

$txt = $ctl->find("input", 0); 

should do the job. 应该做的工作。

SimpleHTMLDOM API reference SimpleHTMLDOM API参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM