PHP获取描述标签DomDocument(PHP Get description tag DomDocument)
我正在尝试为我的SEO系统获取描述元标记。
我正在使用Laravel 5.4
ScanController:
$description = $dom->getElementsByTagName('meta')->getAttribute('description');
指数:
@foreach ($description as $node)
{{$node->nodeValue, PHP_EOL}}
@endforeach
我不熟悉这种编程,所以需要帮助。
解:
我找到了修复!
@foreach ($description as $node)
@if($node->getAttribute('name')=='description')
{{$node->getAttribute('content'), PHP_EOL}}
@endif
@endforeach
我特别寻找这条线:
@if($node->getAttribute('name')=='description')
感谢MA SIDDIQUI在评论中给我一个正确的方向:)
I'm trying to get the description meta tag for my SEO system.
I'm using Laravel 5.4
ScanController:
$description = $dom->getElementsByTagName('meta')->getAttribute('description');
Index:
@foreach ($description as $node)
{{$node->nodeValue, PHP_EOL}}
@endforeach
I'm new to this kind of programming so need help.
Solution:
I found the fix!
@foreach ($description as $node)
@if($node->getAttribute('name')=='description')
{{$node->getAttribute('content'), PHP_EOL}}
@endif
@endforeach
I was especially looking for this line:
@if($node->getAttribute('name')=='description')
Thanks to M A SIDDIQUI in the comments who gave me a kick in the right direction :)
原文:https://stackoverflow.com/questions/42204230
更新时间:2019-08-05 14:29
相关问答
喜欢这个: <?php
$fragment = new DOMDocument();
$fragment->loadHTML($string);
foreach ($fragment->getElementsByTagName("embed") as $element)
{
echo $fragment->saveXML($element);
}
?>
Like this: <?php
$fragment = new DOMDocument();
$fragment->loadHTM
...
您可以使用简单的getElementById: $dom->getElementById('CPHCenter_lblOperandName')->nodeValue
或以选择方式: $selector = new DOMXPath($dom);
$list = $selector->query('/html/body//span[@id="CPHCenter_lblOperandName"]');
echo($list->item(0)->nodeValue);
//or
foreac
...
由于您提供的文档片段不完整,甚至无效,因此我必须进行一些更改。 你说你需要...这个不是有效的元素的值,因为名称和名称空间中的打开和关闭名称是不同的,所以你会期望像... 作为如何从命名空间获取值的示例(使用一些更正的和一些猜测的XML更改)... $xml = <<
...
好吧,你可以使用length参数到DomNodeList ( getElementsByTagName调用返回的内容)。 如果您只想要第一个结果: $nodes = $xmlDoc->getElementsByTagName('city');
if ($nodes->length > 0) {
$city = $nodes->item(0)->nodeValue;
} else {
$city = ''; // There is no city element
}
Well,
...
$description = $dom->getElementsByTagName('meta');
@foreach ($description as $node)
@if ($node->hasAttribute('content'));
{{$node->getAttribute('content'), PHP_EOL}}
@endif
@else
{{ "No meta tag found with content attribute
...
这就是我解决它的方式 /**
* Returns the outer HTML of a certain tag.
* You can decide whether the result should be returned as string or inside an array.
*
* @param $html_string
* @param $tagName
* @param string $return_as
* @return array|string
*/
publi
...
使用DOMDocument类来解析XML。 然后使用getElementsByTagName()获取所有coordinates元素。 $dom = new DOMDocument();
// load file
$dom->load("file.kml");
// get coordinates tag
$coordinates = $dom->getElementsByTagName("coordinates");
foreach($coordinates as $coordinate){
...
字符节点节点没有命名空间。 这有两种类型。 文本节点,它使用特殊语法对XML特殊字符和CDATA节进行编码。 两者都可以用于atom:description和atom:summary 。 预期内容(对于Atom Parser)取决于type属性。 默认只是text , html意味着它需要编码为文本的HTML片段, xhtml是XHTML命名空间中的子节点。 您不应该使用createElement()/createElementNS()的content参数或设置$ nodeValue属性,除非您确
...
试试这个: if(strpos($node->getAttribute('href'), 'some value') !== true){
$temp = new DOMDocument();
$temp->appendChild($temp->importNode($node, true));
$node = $temp->saveHTML();
//var_dump($node);
$anchors[] = $node;
}
Try this: if
...
$links = $dom->getElementsByTagName('meta');
foreach($links as $link){
$name = $link->getAttribute('name');
if($name == 'description'){$description = $link->getAttribute('content');}
}
$links = $dom->getElementsByTagName('meta');
foreach($
...