1

こんにちは、simple_html_dom を使用して、EXACT クラス「hello」を持つタグのすべてのインスタンスを検索しています

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

上記は、「hello world」のようなクラスも提供するため、これを行うわけではありません。はい、配列から正しい要素を数えてリストするのは簡単ですが、解析されているソースhtmlが変更されるため、実用的ではありません。

クラスの正確な用語を見つける方法はありますか?

ありがとう

4

1 に答える 1

2

これを試して:

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.
于 2009-10-07T03:25:56.353 に答える