简体   繁体   中英

PHP Get description tag DomDocument

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 MA SIDDIQUI in the comments who gave me a kick in the right direction :)

$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

eg.

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

You need to foreach the tag and get the attribute and not the nodeValue, i am not much good in laravel so close @else i dont know how to do that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM