Cecil

Html

HTML utility class.

This class provides utility methods for HTML manipulation.

Table of Contents

Methods

getImageCandidates()  : array<string|int, string>
Get candidate image URLs from HTML content, ordered by priority.
getImageFromMetaTags()  : string|null
Get the image URL from Open Graph or Twitter meta tags.
getOpenGraphMetaTags()  : array<string|int, mixed>
Extract Open Graph meta tags from HTML content.
getTwitterMetaTags()  : array<string|int, mixed>
Extract Twitter meta tags from HTML content.
getImagesFromJsonLd()  : array<string|int, string>
Extracts image URLs from JSON-LD data.

Methods

getImageCandidates()

Get candidate image URLs from HTML content, ordered by priority.

public static getImageCandidates(string $html[, string|null $baseUrl = null ]) : array<string|int, string>

Fallbacks (in order):

  1. Open Graph (og:image:secure_url, og:image, og:image:url)
  2. Twitter (twitter:image, twitter:image:src)
  3. <link rel="image_src">
  4. Schema.org microdata (itemprop="image")
  5. JSON-LD image property
  6. First <img> in <article>, <main> or <body>
  7. <link rel="apple-touch-icon">
  8. <link rel="icon">

Relative URLs are resolved against <base href> or $baseUrl.

Parameters
$html : string

The HTML content to parse

$baseUrl : string|null = null

The URL of the HTML document

Return values
array<string|int, string> —

A list of unique absolute image URLs

getImageFromMetaTags()

Get the image URL from Open Graph or Twitter meta tags.

public static getImageFromMetaTags(string $html) : string|null
Parameters
$html : string

The HTML content to parse

Return values
string|null —

The image URL if found, null otherwise

getOpenGraphMetaTags()

Extract Open Graph meta tags from HTML content.

public static getOpenGraphMetaTags(string $html) : array<string|int, mixed>
Parameters
$html : string

The HTML content to parse

Return values
array<string|int, mixed> —

An associative array of Open Graph meta tags

getTwitterMetaTags()

Extract Twitter meta tags from HTML content.

public static getTwitterMetaTags(string $html) : array<string|int, mixed>
Parameters
$html : string

The HTML content to parse

Return values
array<string|int, mixed> —

An associative array of Twitter meta tags

getImagesFromJsonLd()

Extracts image URLs from JSON-LD data.

private static getImagesFromJsonLd(array<string|int, mixed> $data) : array<string|int, string>
Parameters
$data : array<string|int, mixed>
Return values
array<string|int, string>
On this page

Search results