2017-02-13 91 views
0

我正在尝试为我的SEO系统获取描述元标记。PHP获取描述标签DomDocument

我使用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') 

由于M A SIDDIQUI在谁给了我一踢在正确的方向的意见:)

回答

0
$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"}} 
@endforeach 

如。

<meta name="description" content="this is the meta description content" /> 

您需要的foreach的标签,并获取属性,而不是的nodeValue,我不是在laravel如此接近@else我不知道该怎么做,多好。

+0

嘿,谢谢你的回复。我已经试过了,但还是没有收到任何东西。当我将“描述”更改为“内容”时,它提供了一些内容,但不是我想要的内容。你有另一种解决方案吗? – itvba

+0

嗨,请看修改后的答案 –

+0

再次感谢。但它仍然无法正常工作。 它只是说“没有元标签找到....”,但有一个元描述标签。 此外,如果我将描述更改为内容,则会加载所有元标记。 – itvba