How to get meta tags in 2 easy way in PHP

Hi friends, Today we will learn  how to get meta tags in php.We have two ways to get the meta tags.

Note - follow the steps.

1-   Php built-in function   (get_meta_tags).
2 -  PHP - HTML Dom Parser using Curl.

We will learn look into both points.

So, let's Start 

1-  get_meta_tags  - Extracts all meta tag content attributes from a file and returns an array.

Example- 

meta tag inside head section 

<meta content="Short Tutorials" name="author">
<meta content="Short Tutorials -PHP" name="keywords">
<meta content="Short Tutorials -PHP" name="description">
<meta content="blogger" name="generator">

Note:

Only meta tags with name attributes will be parsed. Quotes are not required.


Php code 

<?php

       

     // in my case i want to get 4 meta tags. you can get tag which you want.


      $tags = get_meta_tags('https://shorttutorial007.blogspot.com/');  // pass the url

  

     // it will return all meta tags in array now you can get the value of meta tags which you want. 


        echo $tags['author'];         

        echo $tags['keywords'];    

        echo $tags['description']; 

        echo $tags['generator']; 

?>



Returns Values


Returns an array with all the parsed meta tags.

The value of the name property becomes the key, the value of the content property becomes the value of the returned array, so you can easily use standard array functions to traverse it or access single values. Special characters in the value of the name property are substituted with '_', the rest is converted to lower case. If two meta tags have the same name, only the last one is returned.




2 -  PHP - HTML Dom Parser using Curl.

     Dom parser travels based on tree based and before access the data, it will load the data         into dom object and it will update the data to the web browser.



    We will get title, description and keywords using Curl PHP Dom Parsing.

        function file_get_contents_curl($url)

        {

                $ch = curl_init();


                curl_setopt($ch, CURLOPT_HEADER, 0);

                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

                curl_setopt($ch, CURLOPT_URL, $url);

                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);


                $data = curl_exec($ch);

                curl_close($ch);


       return $data;

    }


        $html = file_get_contents_curl('https://shorttutorial007.blogspot.com/');


        // PHP parsing begins here:

        $doc = new DOMDocument();

        @$doc->loadHTML($html);


        $nodes = $doc->getElementsByTagName('title');  


        //get and display what you need:

        $title = $nodes->item(0)->nodeValue;   // get title 


        $metas = $doc->getElementsByTagName('meta');


        for ($i = 0; $i < $metas->length; $i++)

        {        

                $meta = $metas->item($i);

                if($meta->getAttribute('name') == 'description'){  //check attribute name

                   $description = $meta->getAttribute('content'); //  get description 

                }

              

                if($meta->getAttribute('name') == 'keywords'){   //check attribute name

                   $keywords = $meta->getAttribute('content');  //  get keywords 

                }

                  

         }


            echo "Title: $title". '<br/><br/>';

            echo "Description: $description". '<br/><br/>';

            echo "Keywords: $keywords";


Yeah! You have learned ЁЯШК

I hope you understood this and like this article. if you like it so, please give feedback in the comment section.

If any mistakes I made here, so please let me know and improve me.
If you have any query, feel free to ask me  ЁЯШК 

Recommended post

Post a Comment

Please do not enter any spam link in the comment section.

Previous Post Next Post