Frage

Hallo ich für alle Instanzen von Tags mit der EXACT Klasse bin auf der Suche „Hallo“ mit simple_html_dom

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

Das oben nicht ganz das, weil es gibt mir auch Klassen wie „Hallo Welt“. Es ist einfach, ja zu zählen durch und Liste das richtige Element aus dem Array, aber die Quelle html, die Änderungen werden analysiert wird, so dass nicht praktikabel ist.

Irgendwelche Ideen, wie eine genaue Bezeichnung für die Klasse zu finden?

Danke

War es hilfreich?

Lösung

Versuchen Sie diese:

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

Wenn das nicht funktioniert, können Sie immer tun, um diese weniger elegant, aber immer noch Ansatz arbeiten:

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

    //do stuff here
}

Sie können mehr heraus über diese Art von Sachen unter der Überschrift finden, der sagt Wie HTML-Elemente zu finden? im Handbuch. Attributselektoren sind sehr mächtig, siehe hier:

[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.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top