0

HTML の解析中に ID とタグに基づいて値を取得する方法を知っています。しかし、に基づいて値を取得する方法がわかりませんclassname。これは私が試したことです:

$dom = new DOMDocument();
$dom->loadHTML($html);

$data = $dom->getElementsByTagName($identifier);

foreach ($data as $v) 
{
    $value[] = $v->nodeValue;
}
4

1 に答える 1

0

タグ名で要素を取得しようとしました。あなたがする必要があるのは: -

1.Get all the elements by TagName.
2. Now take the classname from the tagname if exists.
3.Compare the classname wit ur input classname, if exists print that data.

これを試してみてください、私のために働きました:-

$dom = new DOMDocument();
$dom->loadHTML($html);
$new_data = $dom->getElementsByTagName('*');
        $matched = array();

        //echo $data->nodeValue;
        for ($i = 0; $i < $new_data->length; $i++) {

            if (isset($new_data->item($i)->attributes->getNamedItem('class')->nodeValue)) {
                $classname = $new_data->item($i)->attributes->getNamedItem('class')->nodeValue;
                if ($classname == $identifier) {
                    $matched[] = $new_data->item($i)->nodeValue;
                }
            }
        }
        print_r($matched);
于 2013-03-06T07:26:57.817 に答える