PHP - 从属性中提取内容

时间:2013-04-17 06:14:18

标签: php html dom extract

我有问题。我需要从atributte内容中提取内容:

<html>
<head>
<meta name="keywords" content="KEYWORDS">
<meta name="description" content="THIS TEXT"> 
</head>

我使用这个PHP代码:

$doc = new DOMDocument();
@$doc->loadHTML($html);
$tags = $doc->getElementsByTagName('meta');

foreach ($tags as $tag) {
       echo $tag->getAttribute('content');

        }

但是代码只找到了atributte内容的首次出现, 但我需要第二个外观atributte内容......

2 个答案:

答案 0 :(得分:0)

请试试这个

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

$elements = $dom->getElementsByTagName('meta');
foreach ($elements as $child) {
    echo $child->nodeValue;
}

答案 1 :(得分:0)

做这样的事情:

<?php
$nodes = $xml->getElementsByTagName ("meta");

$nodeListLength = $nodes->length; 
for ($i = 0; $i < $nodeListLength; $i ++)
{
   $node = $nodes->item($i)->getAttribute('content');
}
?>