【发布时间】:2012-11-23 08:07:24
【问题描述】:
【问题讨论】:
标签: php attributes wildcard simple-html-dom
【问题讨论】:
标签: php attributes wildcard simple-html-dom
因为简单的 HTML DOM 已经有一种方法来选择包含特定值和/或其他内容的属性。例如
$html->find("div[class*=col]", 0)->outertext
或者您可以像这样检索以col 开头的div 节点
$html->find("div[class^=col]", 0)->outertext
为了安全起见,您可以在此第 3 方插件中找到过滤属性的所有其他方法(顺便说一下,基于 libxml 处理 DOM 有更好的方法,最终列表可以是 @987654321 @)
[attribute] - 匹配具有指定属性的元素。[!attribute] - 匹配没有指定属性的元素。[attribute=value] - 匹配具有特定值的指定属性的元素。[attribute!=value] - 将不具有特定属性的元素与特定值匹配。[attribute^=value] - 匹配具有指定属性并以特定值开头的元素。[attribute$=value] - 匹配具有指定属性并以特定值结尾的元素。[attribute*=value] - 匹配具有指定属性且包含特定值的元素。【讨论】:
我目前没有办法测试它,但是 当我研究它时 (http://simplehtmldom.sourceforge.net/),它应该相当简单。
$html = file_get_html('http://somesite.net');
foreach($html->find('div') as $div){
if stripos($div->class,"col"){
// this $div has a "col" class..
}
}
或者更简单:
$html = file_get_html('http://somesite.net');
foreach($html->find('div.col') as $div){
// every $div has a "col" class..
}
有效吗?
【讨论】:
class="col" 的div,而不是class="col *"