wppaste
WordPress

WP_REST_URL_Details_Controller::get_meta_with_content_elements( string $html ): array

Since
5.9.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-url-details-controller.php:535
Gets all the meta tag elements that have a 'content' attribute.

Compatibility

WordPress
since 5.9.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$htmlstring
The string of HTML to be parsed.

Return value

array
A multidimensional indexed array on success, else empty array.
  • $0string[]

    Meta elements with a content attribute.
  • $1string[]

    Content attribute's opening quotation mark.
  • $2string[]

    Content attribute's value for each meta element.

Performance profile

How much work a call to WP_REST_URL_Details_Controller::get_meta_with_content_elements() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
7

Executed per call on PHP 8.5. The body compiles to 7.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • regexregular expression over the whole inputpreg_match_all()called directly

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_URL_Details_Controller::get_meta_with_content_elements() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always7preg_match_all()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 7 instructions, 7 executed per call, 0 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Used by · 1

Source code

	private function get_meta_with_content_elements( $html ) {		/*		 * Parse all meta elements with a content attribute.		 *		 * Why first search for the content attribute rather than directly searching for name=description element?		 * tl;dr The content attribute's value will be truncated when it contains a > symbol.		 *		 * The content attribute's value (i.e. the description to get) can have HTML in it and be well-formed as		 * it's a string to the browser. Imagine what happens when attempting to match for the name=description		 * first. Hmm, if a > or /> symbol is in the content attribute's value, then it terminates the match		 * as the element's closing symbol. But wait, it's in the content attribute and is not the end of the		 * element. This is a limitation of using regex. It can't determine "wait a minute this is inside of quotation".		 * If this happens, what gets matched is not the entire element or all of the content.		 *		 * Why not search for the name=description and then content="(.*)"?		 * The attribute order could be opposite. Plus, additional attributes may exist including being between		 * the name and content attributes.		 *		 * Why not lookahead?		 * Lookahead is not constrained to stay within the element. The first <meta it finds may not include		 * the name or content, but rather could be from a different element downstream.		 */		$pattern = '#<meta\s' . 				/*				 * Allows for additional attributes before the content attribute.				 * Searches for anything other than > symbol.				 */				'[^>]*' . 				/*				* Find the content attribute. When found, capture its value (.*).				*				* Allows for (a) single or double quotes and (b) whitespace in the value.				*				* Why capture the opening quotation mark, i.e. (["\']), and then backreference,				* i.e \1, for the closing quotation mark?				* To ensure the closing quotation mark matches the opening one. Why? Attribute values				* can contain quotation marks, such as an apostrophe in the content.				*/				'content=(["\']??)(.*)\1' . 				/*				* Allows for additional attributes after the content attribute.				* Searches for anything other than > symbol.				*/				'[^>]*' . 				/*				* \/?> searches for the closing > symbol, which can be in either /> or > format.				* # ends the pattern.				*/				'\/?>#' . 				/*				* These are the options:				* - i : case-insensitive				* - s : allows newline characters for the . match (needed for multiline elements)				* - U means non-greedy matching				*/				'isU'; 		preg_match_all( $pattern, $html, $elements ); 		return $elements;	}

Changelog

Introduced in 5.9.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-includes/rest-api/endpoints/class-wp-rest-url-details-controller.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.