按类名获取DOM元素我正在使用PHP DOM,我正在尝试在DOM节点中获取具有给定类名的元素。获得该子元素的最佳方法是什么?更新:我最终Mechanize用于PHP,这更容易使用。
3 回答
神不在的星期二
TA贡献1963条经验 获得超6个赞
我认为接受的方式更好,但我想这可能也有效
function getElementByClass(&$parentNode, $tagName, $className, $offset = 0) {
$response = false;
$childNodeList = $parentNode->getElementsByTagName($tagName);
$tagCount = 0;
for ($i = 0; $i < $childNodeList->length; $i++) {
$temp = $childNodeList->item($i);
if (stripos($temp->getAttribute('class'), $className) !== false) {
if ($tagCount == $offset) {
$response = $temp;
break;
}
$tagCount++;
}
}
return $response;}- 3 回答
- 0 关注
- 577 浏览
添加回答
举报
0/150
提交
取消
