2012-01-04 72 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")。