【问题标题】:PHP - Extract content from atributtePHP - 从属性中提取内容
【发布时间】:2013-04-17 06:14:17
【问题描述】:

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

<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');

        }

但代码只找到属性内容的第一次出现, 但我需要第二个外观属性内容....

【问题讨论】:

  • 对我来说,它按规定工作。
  • 是的,它也在这里工作

标签: php html dom extract


【解决方案1】:

请试试这个

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

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

【讨论】:

  • This show me error: Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: htmlParseStartTag: misplaced tag in Entity, line: 1. 源代码不是 HTML 有效的,它不是我的源代码。
【解决方案2】:

做这样的事情:

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

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

【讨论】:

  • 这行不通。我认为这段代码只在 HTML 有效页面上运行。
  • 我不这么认为,反正请参考this
猜你喜欢
  • 2018-04-01
  • 2011-12-14
  • 1970-01-01
  • 2015-09-02
  • 1970-01-01
  • 2014-01-04
  • 1970-01-01
  • 2021-01-14
  • 2010-11-10
相关资源
最近更新 更多