2012-01-04 61 views
1

我的代码如下简单的HTML DOM空间在属性

foreach($html->find('div[class=this attribute]') as $itemtitle) { 
    echo $itemtitle; 
} 

我有一个空间,在属性的中间,所以我的代码似乎没有工作。有没有办法绕过这个空间,这样它就可以捡起它?留下的空间。

回答

4

HTML类名实际上不能有空格。空格划分了多个类,因此实际上给了元素两个类:thisattribute

因此,找到两个类的元素。这应该做的伎俩:(该.是类的简写)

foreach($html->find('div.this.attribute') as $itemtitle) { 
    echo $itemtitle; 
} 


其实,你原来的代码发布将正常工作。您的代码中有其他地方出现错误 - $html未包含必要的解析HTML。你能发表你的代码段吗?

+0

类属性可以绝对有空格,但类名不能。 – alexn 2012-01-04 21:42:49

+0

@alexn,很好的电话。修订,所以没有歧义。 – benesch 2012-01-04 21:44:10

+0

当我运行 – user1130861 2012-01-04 23:16:49

2

是的,只是引用它:

foreach($html->find('div[class="this attribute"]') as $itemtitle) { 
    echo $itemtitle; 
} 
0

尝试:

find('div[class="this attribute"]') 
1

既然你正在寻找的类属性,你可以使用:

foreach($html->find('div.this.attribute') as $itemtitle) { 
    echo $itemtitle; 
} 

这将考验,它具有类别thisattribute,但不一定以单数分隔e属性中的空格(可以在其之间,之前和之后有其他类,并且订单可以颠倒,如class="attribute this")。