Meta tags and Open Graph
OgParser and OgDomParser read the same tags into the same Meta fields. OgParser uses regular expressions and needs no extension; OgDomParser parses the document with the HTML5 parser of PHP 8.4 (Dom\HTMLDocument) and is more accurate.
Tags that are read
| Tag | Meta field |
|---|---|
<title>…</title> | setTitle() / getTitle() |
<meta name="description" content="…"> | getDescription() |
<meta name="keywords" content="…"> | getKeywords() (the whole string, not split) |
<meta name="author" content="…"> | getAuthors(): an Author with id() null |
<meta property="og:title" content="…"> | getOgTitle() |
<meta property="og:description" content="…"> | getOgDescription() |
<meta property="og:type" content="…"> | getOgType() |
<meta property="og:url" content="…"> | getOgUrl() |
<meta property="og:site_name" content="…"> | getOgSiteName() |
<meta property="og:image" content="…"> | getOgImage() |
<meta property="article:section" content="…"> | getSections(): a Section with id() null |
<meta property="article:published_time" content="…"> | getPublishedTime(), an absolute date with its offset (Dates) |
<meta property="article:modified_time" content="…"> | getModifiedTime(), an absolute date with its offset (Dates) |
The name tags must use name, the Open Graph tags must use property (that is what the Open Graph protocol specifies): <meta name="og:title"> is not read. Tags that are not in the table, including twitter:*, og:image:width, og:locale and article:tag, are ignored; read them with your own parser.
$meta = new OgDomParser()->parse($html);
echo $meta->getOgType(), ' | ', $meta->getOgSiteName(), ' | ', $meta->getOgUrl(), "\n";
echo $meta->getSections()[0]->name(), ' | ', $meta->getPublishedTime()?->format(DATE_ATOM), "\n";article | Example Blog | https://blog.example/php-84-tips
PHP | 2024-11-21T08:00:00+00:00OgDomParser
$parser = new OgDomParser(); // throws MissingExtensionException without ext-dom or ext-libxml- Any markup. The document is parsed by
Dom\HTMLDocument, the spec-compliant HTML5 parser of PHP 8.4, the same way a browser parses it: attribute order, quoting style, upper-case tags, HTML5 elements (<header>,<main>, custom elements) and missing or misnested closing tags do not matter. Parse errors are not reported (LIBXML_NOERROR), and the parser does not touch yourlibxml_use_internal_errors()setting. - Title: the text of the first HTML
<title>element, as it is (not trimmed). A<title>inside an inline<svg>is not the page title. - Attribute values are case-insensitive:
property="OG:Title"isog:title. - Repeated tags: every
authorandarticle:sectiontag is added to the list; for the other fields the last tag on the page wins. - Entities are decoded exactly once, by the HTML parser, with the full HTML5 list (
…becomes…,čč). A double-escaped&amp;stays the literal text&, like in a browser. - Character set: the charset comes from a byte order mark or from a
<meta charset="windows-1250">/<meta http-equiv="Content-Type" content="text/html; charset=…">declaration in the first 1024 bytes, like in a browser. A page without a declaration is read as UTF-8. When a page is not valid UTF-8 and its declaration comes later, the first<meta>declaration anywhere in the page is used; without one, an XML declaration (<?xml encoding="…"?>) is used. The values are always valid UTF-8: bytes that are not valid in the charset become U+FFFD. TheContent-Typeheader of the response is not used:parse()only sees the HTML. - Empty input:
parse('')returns an emptyMeta.
OgParser
$parser = new OgParser(); // no requirements- Regular expressions: one expression per tag, case-insensitive, over the whole string. It works on any string, including fragments such as only the
<head>. - Attribute order matters: the
nameorpropertyattribute must come beforecontentin the same tag. Single and double quotes both work (a value may contain the other kind of quote and>); unquoted values do not. - One tag at a time: every expression stays inside one
<meta>tag. A tag that cannot be read, such as<meta content="…" property="og:title">(content first) or a tag withoutcontent, givesnull, never the value of another tag. An emptycontent=""gives an empty string. - First match only: for every tag, including
authorandarticle:section, only the first occurrence is read. - Entities: every named and numeric HTML5 entity is decoded once with
html_entity_decode()(ébecomesé,……,––, a no-break space U+00A0), in the title and in everycontent. A double-escaped&amp;becomes the literal text&. Unknown entities and entities without the closing;stay as they are. - Character set: the bytes are returned as they are; convert pages that are not UTF-8 before parsing (Fetching pages).
Content first
OgParser does not read tags written as <meta content="…" property="og:title">; OgDomParser does. The broken pages example compares the two. Use OgDomParser for pages you do not control.
The two side by side
The Open Graph article example parses one news article with both parsers. The output differs only in the sections: the page has two article:section tags and OgParser reads the first one.
$html = '<title>A & B …</title>'
. '<meta property="og:title" content="First"><meta property="og:title" content="Second">'
. '<meta name="author" content="Ann"><meta name="author" content="Bob">';
foreach ([new OgParser(), new OgDomParser()] as $parser) {
$meta = $parser->parse($html);
$authors = implode(', ', array_map(static fn(Author $a): ?string => $a->name(), $meta->getAuthors()));
echo new ReflectionClass($parser)->getShortName(), ' | ', $meta->getTitle(), ' | ', $meta->getOgTitle(), ' | ', $authors, "\n";
}OgParser | A & B … | First | Ann
OgDomParser | A & B … | Second | Ann, Bob