Getting title tag and meta tags from a website using PHP

14 May 2024

I was developing a SEO tool, where I needed to gather information about websites title and description.

Getting metadata with get_meta_tags

The get_meta_tags function extracts all meta tag content attributes from a website.

The function does not however extract the contents of the title tag, so there we have to utilize another method.

Using DOMDocument to extract information from HTML

Using DOMDocument we can easily extract the contents of the title tag from the websites HTML. The method is quite similar to how I webscrape in Python using BeautifulSoup. You can extract the contents of the title tag like this:
$html = file_get_contents($url);
$doc = new \DOMDocument();
$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');
$title = $nodes->item(0)->nodeValue;
Using this method we can also continue our data collection and grab the description or other metadata. This can be achieved like this:
$metas = $doc->getElementsByTagName('meta');

for ($i = 0; $i < $metas->length; $i++)
{
    $meta = $metas->item($i);
    if ($meta->getAttribute('name') == 'description')
    {
        $description = $meta->getAttribute('content');
    }
}

You might also enjoy

The Seven Restful Controller Actions in Laravel

The Seven Restful Controller Actions in Laravel

Published 2024-05-05 — Updated 2024-05-16

Laravel

PHP

Web development

The seven RESTful actions and how you could implement them in Laravel

Read the post →
Simplifying Your Code with Advanced Operators in PHP

Simplifying Your Code with Advanced Operators in PHP

Published 2024-05-05

PHP

Web development

Ready to write PHP code like a pro? Learn how to use advanced operators to make your code more concise and readable, and take your skills to the next level.

Read the post →
Useful Laravel packages

Useful Laravel packages

Published 2024-05-05

Laravel

PHP

Web development

Learn what packages that can help you build even better websites in Laravel. We'll go trhough the must-haves and packages that are just nice to have.

Read the post →