2017-02-13 94 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