simple_html_dom — проблема, не описанная в руководстве

StackOverflow https://stackoverflow.com/questions/1528519

  •  20-09-2019
  •  | 
  •  

Вопрос

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

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

Вышеупомянутое не совсем этого делает, потому что оно также дает мне такие классы, как «привет, мир».Да, просто посчитать и перечислить правильный элемент из массива, но анализируемый исходный HTML-код изменяется, поэтому это непрактично.

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

Спасибо

Это было полезно?

Решение

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

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.
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top