views:

165

answers:

1

hello I am searching for all instances of tags with the EXACT class "hello" using simple_html_dom

foreach($html->find('.hello')as $found

The above doesn't quite do this because it also gives me classes like "hello world". It is simple yes to count through and list the correct element from the array but the source html that is being parsed changes so that's not practical.

Any ideas how to find an exact term for the class?

Thanks

+2  A: 

Try this:

foreach($html->find('[class=hello]') as $found)

If that doesn't work, you could always do this less elegant but still working approach:

foreach($html->find('.hello') as $found)
{
    if ($found->class != 'hello')
        continue;

    //do stuff here
}

You can find more out about this kind of stuff under the heading that says How to find HTML elements? in the manual. Attribute selectors are very powerful, see here:

[attribute]           Matches elements that have the specified attribute.
[attribute=value]    Matches elements that have the specified attribute with a certain value.
[attribute!=value]  Matches elements that don't have the specified attribute with a certain value.
[attribute^=value]  Matches elements that have the specified attribute and it starts with a certain value.
[attribute$=value]  Matches elements that have the specified attribute and it ends with a certain value.
[attribute*=value]  Matches elements that have the specified attribute and it contains a certain value.
ryeguy
Thanks that did it.Took a while to respond because the computer wasn't letting me add comment or give tick.
David Willis