wppaste
WordPress

block_core_post_time_to_read_word_count( string $text, string $type ): string

Since
6.9.0
Source
wp-includes/blocks/post-time-to-read.php:28
Counts words or characters in a provided text string.

Description

This function currently employs an array of regular expressions to parse HTML and count words, which may result in inaccurate word counts. However, it is designed primarily to agree with the corresponding JavaScript function.

Any improvements in the word counting, for example with the HTML API and IntlBreakIterator::createWordInstance() should coordinate with changes to the JavaScript implementation to ensure consistency between the editor and the rendered page.

Compatibility

WordPress
since 6.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 3 of the 5 tracked releases, added in 6.9.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$textstring
Text to count elements in.
$typestring
The type of count. Accepts 'words', 'characters_excluding_spaces', or 'characters_including_spaces'.

Return value

string
The rendered word count.

Performance profile

How much work a call to block_core_post_time_to_read_word_count() 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
6–40

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 47.

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 · 2 distinct outcomes

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

WhenInstructionsCalls it makes
always6none
always33–40preg_match_all()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev476–404
8.5476–404
8.4476–40426 fewer instructions than PHP 8.3
8.3738–604
8.2738–604
8.1738–604
7.4738–604

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

function block_core_post_time_to_read_word_count( $text, $type ) {	$settings = array(		'html_regexp'                        => '/<\/?[a-z][^>]*?>/i',		'html_comment_regexp'                => '/<!--[\s\S]*?-->/',		'space_regexp'                       => '/&nbsp;|&#160;/i',		'html_entity_regexp'                 => '/&\S+?;/',		'connector_regexp'                   => "/--|\x{2014}/u",		'remove_regexp'                      => "/[\x{0021}-\x{0040}\x{005B}-\x{0060}\x{007B}-\x{007E}\x{0080}-\x{00BF}\x{00D7}\x{00F7}\x{2000}-\x{2BFF}\x{2E00}-\x{2E7F}]/u",		'astral_regexp'                      => "/[\x{010000}-\x{10FFFF}]/u",		'words_regexp'                       => '/\S\s+/u',		'characters_excluding_spaces_regexp' => '/\S/u',		'characters_including_spaces_regexp' => "/[^\f\n\r\t\v\x{00AD}\x{2028}\x{2029}]/u",	); 	$count = 0; 	if ( '' === trim( $text ) ) {		return $count;	} 	// Sanitize type to one of three possibilities: 'words', 'characters_excluding_spaces' or 'characters_including_spaces'.	if ( 'characters_excluding_spaces' !== $type && 'characters_including_spaces' !== $type ) {		$type = 'words';	} 	$text .= "\n"; 	// Replace all HTML with a new-line.	$text = preg_replace( $settings['html_regexp'], "\n", $text ); 	// Remove all HTML comments.	$text = preg_replace( $settings['html_comment_regexp'], '', $text ); 	// If a shortcode regular expression has been provided use it to remove shortcodes.	if ( ! empty( $settings['shortcodes_regexp'] ) ) {		$text = preg_replace( $settings['shortcodes_regexp'], "\n", $text );	} 	// Normalize non-breaking space to a normal space.	$text = preg_replace( $settings['space_regexp'], ' ', $text ); 	if ( 'words' === $type ) {		// Remove HTML Entities.		$text = preg_replace( $settings['html_entity_regexp'], '', $text ); 		// Convert connectors to spaces to count attached text as words.		$text = preg_replace( $settings['connector_regexp'], ' ', $text ); 		// Remove unwanted characters.		$text = preg_replace( $settings['remove_regexp'], '', $text );	} else {		// Convert HTML Entities to "a".		$text = preg_replace( $settings['html_entity_regexp'], 'a', $text ); 		// Remove surrogate points.		$text = preg_replace( $settings['astral_regexp'], 'a', $text );	} 	// Match with the selected type regular expression to count the items.	return (int) preg_match_all( $settings[ $type . '_regexp' ], $text );}

Changelog

Introduced in 6.9.0. Unchanged from 6.9.7 through 7.1.0.

  1. 6.9.7
  2. 7.0.4
  3. 7.1.0

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

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/blocks/post-time-to-read.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.