martinille / meta-tag-extraction
PHP library for fetching and parsing meta tags from web pages using a given URL or HTML source.
2.1.1
2025-04-28 14:34 UTC
Requires
- php: >=8.1
- ext-dom: *
- guzzlehttp/guzzle: ^7.9
- psr/simple-cache: ^3.0
Requires (Dev)
- nyholm/psr7: ^1.8
- php-coveralls/php-coveralls: ^0.4.0
- phpunit/phpunit: ^10.5
- symfony/cache: ^6.4
- symfony/http-client: ^6.4
This package is auto-updated.
Last update: 2025-05-28 14:54:29 UTC
README
A light-weight library for extracting meta tags from URL or HTML content.
It allows you to:
- get all meta tags from URL or HTML content (including
title
,charset
, andhtml[lang]
attribute), - set custom cache provider for caching the results of web scraping (using PSR-16
Psr\SimpleCache\CacheInterface
), - set custom HTTP client for web scraping (using PSR-18
Psr\Http\Client\ClientInterface
),
This library can be considered as an extended alternative for get_meta_tags() function.
Requirements
- PHP 8.1 or higher
- PHP DOM extension
Installation
composer require martinille/meta-tag-extraction
Usage
use MartinIlle\MetaTagExtraction\MetaTagExtraction; $url = 'https://example.com'; $metaTagExtraction = new MetaTagExtraction(); $metaTags = $metaTagExtraction->extractFromUrl($url); foreach ($metaTags as $tag) { echo $tag->getName() . ': ' . $tag->getValue() . PHP_EOL; } /* output: charset: utf-8 content-type: text/html; charset=utf-8 viewport: width=device-width, initial-scale=1 title: Example Domain */
Examples
- Extracting meta tags from HTML string: docs/MetaTagExtraction_html_example.php
- Extracting meta tags from a URL: docs/MetaTagExtraction_url_example.php
More examples: docs/index.md
Testing
Unit tests:
composer test
Unit tests with coverage:
composer test:coverage