PHP 7 Script to Scrape or Extract Title and Meta Description of Website URL Full Project For Beginners

  • Post author:
  • Post category:PHP
  • Post comments:0 Comments

 

Welcome folks today in this blog post we will be scraping or extracting the title and meta description of website url in php 7. All the full source code of the application is shown below.

 

 

 

Get Started

 

 

 

In order to get started you need to make an index.php file and copy paste the following code

 

 

index.php

 

 

Extracting Meta Description of Website

 

 

<?php
$meta_tags = get_meta_tags('http://www.example.com/page');
if (isset($meta_tags['description'])) {
echo $meta_tags['description'];
}
else {
echo "Meta description not exists";
}
?>

 

See also  How to Validate YouTube Video URL Format in PHP using parse_url() function in Browser Full Project For Beginners

 

Extracting the title of website

 

 

<?php
  function get_title($url) {
        $fp = file_get_contents($url);
        if (!$fp) 
            return null;
        $res = preg_match("/<title>(.*)<\/title>/siU", $fp, $title_matches);
        if (!$res) 
            return null; 
        $title = preg_replace('/\s+/', ' ', $title_matches[1]);
        $title = trim($title);
        return $title;
    }
echo get_title("http://www.example.com/page");
?>

 

Leave a Reply