simple_html_dom - проблема не описана в руководстве - PullRequest
1 голос
/ 07 октября 2009

привет Я ищу все экземпляры тегов с EXACT-классом "привет", используя simple_html_dom

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

Вышеприведенное не совсем так, потому что оно также дает мне такие классы, как "Привет, мир". Да, просто просчитать и перечислить правильный элемент из массива, но исходный html, который анализируется, меняется, так что это не практично.

Есть идеи, как найти точный термин для класса?

Спасибо

1 Ответ

2 голосов
/ 07 октября 2009

Попробуйте это:

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

Если это не сработает, вы всегда можете сделать этот менее изящный, но все еще работающий подход:

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

    //do stuff here
}

Подробнее об этом можно узнать под заголовком Как найти элементы HTML? в руководстве. Селекторы атрибутов очень мощные, смотрите здесь:

[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.
...