php get site metat tags

Solutions on MaxInterview for php get site metat tags by the best coders in the world

showing results for - "php get site metat tags"
Gabby
31 Nov 2019
1function getSiteOG( $url, $specificTags=0 ){
2  $doc = new DOMDocument();
3  @$doc->loadHTML(file_get_contents($url));
4  $res['title'] = $doc->getElementsByTagName('title')->item(0)->nodeValue;
5
6  foreach ($doc->getElementsByTagName('meta') as $m){
7      $tag = $m->getAttribute('name') ?: $m->getAttribute('property');
8      if(in_array($tag,['description','keywords']) || strpos($tag,'og:')===0) $res[str_replace('og:','',$tag)] = $m->getAttribute('content');
9  }
10  return $specificTags? array_intersect_key( $res, array_flip($specificTags) ) : $res;
11}
similar questions
php meta