user1130861
user1130861

Reputation: 45

Simple HTML Dom Space in attribute

my code is the following

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

I have a space in the middle of the attribute and so my code seems to not be working. Is there a way to get around the space so it picks it up? The space as to stay.

Upvotes: 1

Views: 1480

Answers (4)

benesch
benesch

Reputation: 5269

HTML class names actually cannot have spaces. Spaces delimit multiple classes, so you're actually giving the element two classes: this and attribute.

So, find elements with both classes. This should do the trick:

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

(The . is a shorthand for class.)


Actually, your original code as posted will work fine. You've got an error somewhere else in your code—$html does not contain the necessary parsed HTML. Could you post that section of your code?

Upvotes: 4

Paul
Paul

Reputation: 141877

Since you are looking at the class attribute you could use:

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

Which will test that it has both the classes this and attribute, but not necessarily separated by a single space in the attribute (could have other classes in between, before, and after, and the order may be reversed like class="attribute this").

Upvotes: 1

Ayman Safadi
Ayman Safadi

Reputation: 11562

Try:

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

Upvotes: 0

alexn
alexn

Reputation: 58992

Yes, just quote it:

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

Upvotes: 2

Related Questions