and

How to Get Title and Metadata (meta tags) from URL using PHP

In this article, I am going to show you how to get webpage Titles and Meta tags from external website URLs using PHP. Scroll down to the page so you can see the full source code available for this.

Mostly, All external websites are used to specify the 3 common metadata for the web page are page title, page description, and page keywords. These all meta tags information fetched using PHP. The page description and page keywords within tag and page title is tag.

 
codeat21
loadHTML($data); // Parse DOM to get Title data $nodes = $dom->getElementsByTagName('title'); $title = $nodes->item(0)->nodeValue; // Parse DOM to get meta data $metas = $dom->getElementsByTagName('meta'); $description = ''; $keywords = ''; $site_name = ''; $image = ''; for($i=0; $ilength; $i++)< $meta = $metas->item($i); if($meta->getAttribute('name') == 'description')< $description = $meta->getAttribute('content'); > if($meta->getAttribute('name') == 'keywords')< $keywords = $meta->getAttribute('content'); > if($meta->getAttribute('property') == 'og:site_name')< $site_name = $meta->getAttribute('content'); > if($meta->getAttribute('property') == 'og:image')< $image = $meta->getAttribute('content'); > > echo "Title: $title". '

'; echo "Description: $description". '

'; echo "Keywords: $keywords". '

'; echo "site_name: $site_name". '

'; echo "image: $image"; ?>

Output

  • Title: Home — codeat21.com — Web Designing and Development Tutorials Website.
  • Description: codeat21.com is a web designing and development tutorials website. Learn Web Development, HTML, CSS, PHP, MySQL, JavaScript, Node JS, React JS, jQuery, etc..
  • Keywords:
  • site_name: codeat21.com — Web Designing and Development Tutorials Website.
  • image:
Читайте также:  Vs code python virtualenv настройка
  • Get meta tags from URL
  • Get meta tags from url javascript
  • Get_meta_tags
  • how to add meta tags in php website
  • How to add meta tags dynamically in PHP
  • Php get title from url
  • How to add meta title and description in php
  • php get page title from url, curl get meta tags
  • get og meta tags javascript
  • dynamically change og meta tags

Recent Posts

Источник

Get Webpage Title and Meta Description from URL in PHP

Hi! Today we’ll see how to get webpage title and description from url in php. At times you may want to scrape some url and retrieve the webpage contents. You may need some third-party DOM Parser for this sort of task but PHP is uber-smart and provides you with some fast native solutions. Retrieving page title and meta description tags from url is lot easier than you think. Here we’ll see how to do it.

A Web Page title can be found between the tag and description within tag. Page title is self-explanatory and meta tags store various useful information about a webpage like title, description, keywords, author etc.

php-get-web-page-title-description-from-url

PHP Code to Get Webpage Title from URL:

In order to get/retrieve web page title from url, you have to use function file_get_contents() and regular expression together. Here is the php function to retrieve the contents found between tag.

]*>(.*?)/ims', $page, $match) ? $match[1] : null; return $title; > // get web page title echo 'Title: ' . getTitle('http://www.w3schools.com/php/'); // Output: // Title: PHP 5 Tutorial ?>

PHP Code to Get Webpage Meta Description from URL:

Get/Retrieve meta description from webpage is even more easier with php’s native get_meta_tags() method. The function get_meta_tags() extracts all the meta tag content attributes from any file/url and returns it as an array.

Читайте также:  Rfr epyfnm dthcb java

Here is the php function to get the meta description from url.

 // get web page meta description echo 'Meta Description: ' . getDescription('http://www.w3schools.com/php/'); // Output: // Meta Description: Well organized and easy to understand Web bulding tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, and XML. ?>

Some WebPages may miss meta tags and the above function will return null value if it is not present.

Also you can retrieve the rest of the meta tags in the same way.

We have seen how to get webpage title and meta description from url in php. I hope you enjoy this tutorial. If you have any queries please let me know through your comments.

Источник

Read meta description and title tag of a web page using PHP

Meta description and title tag are two important SEO parameter of any type of site. Every web page that needs to be rank better on search engines, must have meta description tag and title tag. Search engines like Google, Bing, YaHoo give the most value to these two SEO parameters.

Below is a given example which shows you how meta description tag looks like:

And the title tag looks like below example:

How to read the meta description and title tag of a web page content using PHP?

Now I am going to tell you the way of reading meta description and title tag using the popular web development language PHP. Several times it may be needed to get the meta description and title tag content.

Getting title tag means you have to get the text content between . And getting meta tag content means getting the content that you can see inside quotation like content=”meta description content”.

Reading meta description using PHP

Below is the given PHP code snippet to read the meta description tag:

If you run the above code snippet, then it will display meta description text content from the page www.example.com/page. Now below is the modified code of the above code snippet to check if meta description exists on the web page or not using PHP if condition:

Get title tag content text using PHP

The PHP code to read title tag is given below:

(.*)/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"); ?>

Источник

How to get Title and Meta tags from URL using PHP

Today we’ll explain to you how to get Title and Meta tags from URL using PHP.

Checkout more articles on PHP

Get Meta tags using in-built function

Using the get_meta_tags() function we can extract the meta tags from the webpage. We need to pass the url in this function. This function returns the all tags until the closing tag.

Let’s take an example for better understanding. Suppose we have an HTML head section as shown below.

Get Title and Meta tags using cURL

In the above function, we can only get the meta tags using the get_meta_tags() function but can’t get the tag. So now we will show you how to get the title and meta tags using cURL in PHP.

That’s it for today.
Thank you for reading. Happy Coding. 🙂

You may also like.

Sorting Arrays in PHP - Clue Mediator

Sorting Arrays in PHP

Convert CSV to JSON in PHP - Clue Mediator

Convert CSV to JSON in PHP

Form Validation using jQuery in PHP - Clue Mediator

Form Validation using jQuery in PHP

How to use session in PHP - Clue Mediator

How to use session in PHP

Send an email using PHP mail() function - Clue Mediator

Send an email using PHP mail() function

How to fix phpMyAdmin error - Incorrect format parameter - Clue Mediator

How to fix phpMyAdmin error – Incorrect format parameter

Leave a Reply Cancel reply

Search your query

Recent Posts

  • Listing tables and their structure with the MySQL Command Line July 25, 2023
  • How to Import an .sql File into a Remote Server from a Local Machine July 24, 2023
  • How to Copy a File from/to a Remote Server using Command Line July 23, 2023
  • Create a MySQL Database on Linux via Command Line July 22, 2023
  • Connect to a MySQL Database Using the MySQL Command: A Comprehensive Guide July 16, 2023

Tags

Join us

Top Posts

Explore the article

We are not simply proficient at writing blog post, we’re excellent at explaining the way of learning which response to developers.

For any inquiries, contact us at [email protected] .

  • We provide the best solution to your problem.
  • We give you an example of each article.
  • Provide an example source code for you to download.
  • We offer live demos where you can play with them.
  • Quick answers to your questions via email or comment.

Clue Mediator © 2023. All Rights Reserved.

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.

Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.

Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.

Источник

Оцените статью