我正在使用phpquery从网页中提取一些数据。我需要识别页面的菜单。我的实现是找到sibilings> 0且last-child为的每个元素 "a"
。我的代码是:
foreach($this->doc['*'] as $tagObj){
$tag = pq($tagObj);
if(count($tag->siblings()) > 0){
if($tag->find(":last-child")->tagName === "a")
echo trim(strip_tags($tag->html())) . "<br/>";
}
}
但是,我没有得到任何输出因为
$标签 - >找到( “:最后的孩子”) - >标签名
哪个没有返回任何东西。这是什么原因?
我不知道这个库,但也许是这样的
$siblings = $tag->siblings();
if (($siblingCount = count($siblings)) && $siblings[$siblingCount - 1]->tagName === 'a') {
echo ...
}
你可以反向检查 a:last-child
:
例如 :
foreach($this->doc['*'] as $tagObj){
$tag = pq($tagObj);
if(count($tag->siblings()) > 0){
if($tag->find("a:last-child"))
echo trim(strip_tags($tag->html())) . "<br/>";
}
}
这将检查 a
标签 last-child
并且您可以轻松获得其内容。
愿这对你有所帮助。
也许你应该使用 :持续 代替 :最后一个孩子
根据 图书馆谷歌页面:
$li = null;
$doc['ul > li']
->addClass('my-new-class')
->filter(':last') // <--- :last
->addClass('last-li')
// save it anywhere in the chain
->toReference($li);
因为 phpQueryObject
归来的 pq
实现 Iterator
并使用公共数组 $elements
要存储所有元素,我们需要使用它来获取元素 get()
函数,返回一个 DOMElement
那是有的 tagName
和 nodeName
特性:
$q = phpQuery::newDocumentHTML('<div><span class="test-span">Testing test</span></div>');
echo $q->find('.test-span')->get(0)->tagName; // outputs "span"
//echo $q->find('.test-span')->get(0)->nodeName; // outputs "span"
两个属性都将输出具有的标记名称 test-span
当然是这样的课程 span
。