Pergunta

Olá, estou procurando todas as instâncias de tags com a classe exata "Hello" usando simples_html_dom

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

O exposto acima não faz isso porque também me dá aulas como "Hello World". É simples sim contar e listar o elemento correto da matriz, mas a fonte HTML que está sendo analisada muda para que isso não seja prático.

Alguma idéia de como encontrar um termo exato para a classe?

Obrigado

Foi útil?

Solução

Experimente isso:

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

Se isso não funcionar, você sempre poderá fazer isso menos elegante, mas ainda funcionando:

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

    //do stuff here
}

Você pode descobrir mais sobre esse tipo de coisa sob o título que diz Como encontrar elementos HTML? no manual. Os seletores de atributos são muito poderosos, veja aqui:

[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.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top