A reliable way to smooth the name, description and keywords

I am currently using CURL to clean my website. I want to reliably get a title, description and keywords.

//Parse for the title, description and keywords
if (strlen($link_html) > 0)
{
    $tags = get_meta_tags($link);     // name
    $link_keywords = $tags['keywords'];     // php documentation
    $link_description = $tags['description'];
}

The only problem is that now users use all kinds of meta tags, such as an open graph <meta property="og:title" content="The Rock" />. They also strongly distinguish between tags <title> <Title> <TITLE> <tiTle>. It is very difficult to get this reliable data.

I really need code that will retrieve these variables sequentially. If there is any name, keyword and description, provided that he finds it. Because now it seems that he is very amazed and bored.

Perhaps a way to extract all the headers into a caption array? . The scrambling web developer can then choose the best one to write to his database. The same applies to keywords and descriptions.

This is not a duplicate. I did a search through stackoverflow and nowhere does this solution put all the "title", "keywords" and Tags of type "description" in arrays.

+4
source share
1 answer

As a rule, it get_meta_tags()should provide you with most of what you need, you just need to set up a set of cascading checks that will display the required field from each metadata system until they are found. For example, something like this:

function get_title($url) {
  $tags = get_meta_tags($url);
  $props = get_meta_props($url);
  return @tags["title"] || @props["og:title"] || ...
}

, , (, , URL ), get_meta_props() - pcre_* DOMDocument.

, - ! , - "", github

composer require embed/embed
+1

Source: https://habr.com/ru/post/1621063/


All Articles