simple_html_dom - problema no cubierto en el manual
-
20-09-2019 - |
Pregunta
hola Estoy buscando todas las instancias de las etiquetas con la clase exacta "hola" usando simple_html_dom
foreach($html->find('.hello')as $found
Lo anterior no sigue haciendo esto porque también me da clases como "hola mundo". Es simple sí para contar a través de la lista y el elemento correcto de la matriz, pero el código fuente HTML que se está analizando cambios de manera que no es práctico.
¿Alguna idea de cómo encontrar un término exacto para la clase?
Gracias
Solución
Prueba esto:
foreach($html->find('[class=hello]') as $found)
Si eso no funciona, siempre se puede hacer este enfoque menos elegante pero sigue trabajando:
foreach($html->find('.hello') as $found)
{
if ($found->class != 'hello')
continue;
//do stuff here
}
Puede encontrar más provecho de este tipo de cosas en el epígrafe que dice Como encontrar elementos HTML? en el manual. Selectores de atributos son muy poderosos, ver aquí:
[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.