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
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
- Scaling
- Constant
- Instructions
- 6–40
- Plugin surface
- None
- Called by
- 1
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 47.
Nothing here hands control to plugin code.
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 input
preg_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6 | none |
| always | 33–40 | preg_match_all() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 47 | 6–40 | 4 | |
| 8.5 | 47 | 6–40 | 4 | |
| 8.4 | 47 | 6–40 | 4 | 26 fewer instructions than PHP 8.3 |
| 8.3 | 73 | 8–60 | 4 | |
| 8.2 | 73 | 8–60 | 4 | |
| 8.1 | 73 | 8–60 | 4 | |
| 7.4 | 73 | 8–60 | 4 |
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
- render_block_core_post_time_to_read()Renders the `core/post-time-to-read` block on the server.
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' => '/ | /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.
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.