When you work with websites or creating web-scripting applications, you often need to analyze their SEO data. For example, you are a digital marketer who wants to compare how competitors optimize their websites. Instead of checking each website manually, you can write a script to process their entire sitemap and store it into your database for analysis. This post will show you how to Fetch Website SEO Meta Tags with PHP and turn a simple URL input into structured SEO data.
With meta information or tags, you can easily retrieve details like title, description, keywords, and other tags that describe the content of a page for crawlers. It’s a great way to study competitors, verify your own site’s SEO proctice, or build tools for content optimization for search engines.
Why Meta Tags Are Used
Meta tags are HTML elements that provide information about a web page to search engines and browsers. Most of the meta information don’t appear on the visible part of the website but play a crucial role in explaining your content to search engines or systems.
They help define the title that appears in search results, describe the page content, and even explain social media platforms display shared links and what it should show. With optimizing meta tags correctly can improve visibility, click-through rates, and indexing accuracy.
Most Common Meta Tags
Here are some of the most common meta tags you’ll encounter:
- Title Tag – Shows the title of the page in search engine results and browser windows.
- Meta Description – Gives a short description of the page content.
- Meta Keywords – Specifies important keywords (although not as used now).
- Meta Robots – Tells search engines how to crawl or index a page.
- Canonical Tag – Specifies the best version of a page to prevent duplicate content.
- Open Graph Tags – Utilized by social media sites to post previews.
- Twitter Card Tags – Decide what links look like when tweeted on Twitter.
Fetch Meta Information Using PHP Built-in Function
PHP provides a simple yet effective built-in function called get_meta_tags() to retrieve meta information directly from a web page or URL. Let’s take an example to get meta information from URL using PHP script. In all following example, you will take URL as user input and display meta information to user them self.
<!DOCTYPE html>
<html>
<head>
<title>Fetch Website SEO Meta Tags with PHP</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-4">
<div class="container">
<h2 class="mb-4">Fetch Website SEO Meta Tags with PHP</h2>
<form method="post">
<div class="mb-3">
<input type="text" name="url" class="form-control" placeholder="Enter website URL" required value="<?php if(!empty($_POST['url'])) echo $_POST['url']; ?>">
</div>
<button type="submit" class="btn btn-primary">Fetch Meta Tags</button>
</form>
<?php
if (!empty($_POST['url'])) {
$url = $_POST['url'];
$tags = get_meta_tags($url);
if ($tags) {
echo '<h4 class="mt-4">Meta Information for: ' . htmlspecialchars($url) . '</h4>';
echo '<table class="table table-bordered mt-3"><thead><tr><th>Key</th><th>Value</th></tr></thead><tbody>';
foreach ($tags as $key => $value) {
echo '<tr><td>' . htmlspecialchars($key) . '</td><td>' . htmlspecialchars($value) . '</td></tr>';
}
echo '</tbody></table>';
} else {
echo '<p class="text-danger mt-3">No meta tags found or invalid URL.</p>';
}
}
?>
</div>
</body>
</html>
As output, this will show similar result for our site.

Get Meta Tags Using DOMDocument
The DOMDocument class in PHP is a powerful tool that allows you to work and manipulate HTML or XML documents. DOMDocument loads the entire webpage to it’s tree format and access, modify, or extract specific elements like meta tags, titles, or links. It offers more flexibility and control when working with HTML content.
<!DOCTYPE html>
<html>
<head>
<title>Get Meta Tags Using DOMDocument</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-4">
<div class="container">
<h2 class="mb-4">Get Meta Tags Using DOMDocument</h2>
<form method="post">
<div class="mb-3">
<input type="text" name="url" class="form-control" placeholder="Enter website URL" required value="<?php if(!empty($_POST['url'])) echo $_POST['url']; ?>">
</div>
<button type="submit" class="btn btn-primary">Fetch Meta Tags</button>
</form>
<?php
if (!empty($_POST['url'])) {
$url = $_POST['url'];
$html = file_get_contents($url);
$doc = new DOMDocument();
@$doc->loadHTML($html);
$tags = $doc->getElementsByTagName('meta');
echo '<h4 class="mt-4">Meta Information for: ' . htmlspecialchars($url) . '</h4>';
echo '<table class="table table-bordered mt-3"><thead><tr><th>Key</th><th>Value</th></tr></thead><tbody>';
foreach ($tags as $tag) {
if ($tag->getAttribute('name')) {
echo '<tr><td>' . htmlspecialchars($tag->getAttribute('name')) . '</td><td>' . htmlspecialchars($tag->getAttribute('content')) . '</td></tr>';
}
}
echo '</tbody></table>';
}
?>
</div>
</body>
</html>
It also prints same information like previous method but use different mechanisam.
Retrieve Meta Information Using Regular Expression
Another way to get website SEO meta information with PHP is by using regular expressions. While it’s not as reliable as DOM parsing, it works well for small tasks or custom patterns. However, regular expression allows you to fetch additional data which is not presented using meta tags like title tag value favicon icon or any other information.
Let’s take an example to get meta information using regular expression (Regex) in PHP:
<!DOCTYPE html>
<html>
<head>
<title>Retrieve Meta Information using Regex</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-4">
<div class="container">
<h2 class="mb-4">Retrieve Meta Information using Regex</h2>
<form method="post">
<div class="mb-3">
<input type="text" name="url" class="form-control" placeholder="Enter website URL" required value="<?php if(!empty($_POST['url'])) echo $_POST['url']; ?>">
</div>
<button type="submit" class="btn btn-primary">Fetch Meta Tags</button>
</form>
<?php
if (!empty($_POST['url'])) {
$url = $_POST['url'];
$html = file_get_contents($url);
preg_match_all('/<meta\s+name=["\']?([^"\']+)["\']?\s+content=["\']?([^"\']+)["\']?/i', $html, $matches, PREG_SET_ORDER);
echo '<h4 class="mt-4">Meta Information for: ' . htmlspecialchars($url) . '</h4>';
echo '<table class="table table-bordered mt-3"><thead><tr><th>Key</th><th>Value</th></tr></thead><tbody>';
foreach ($matches as $meta) {
echo '<tr><td>' . htmlspecialchars($meta[1]) . '</td><td>' . htmlspecialchars($meta[2]) . '</td></tr>';
}
echo '</tbody></table>';
}
?>
</div>
</body>
</html>
Conclusion
Retrieving SEO information is essential for web developers, marketers, and SEO professionals who wish to know how pages are optimized. With these PHP approaches, you can fetch website SEO meta tags with PHP effectively, either using built-in functions, DOM parsing, or regular expressions.
If you need to increase user interaction on your website, don’t forget our in-depth article on Implement Social Share Buttons in PHP. It will make it easy to add share functionality for well-known social media sites and increase visibility of your content.