WP_Token_Map::read_token( string $text, int $offset = 0, $matched_token_byte_length = null, string $case_sensitivity = 'case-sensitive' ): string|null
- Since
- 6.6.0
- Source
wp-includes/class-wp-token-map.php:534
false.Description
This function returns the translated string, but accepts an optional parameter $matched_token_byte_length, which communicates how many bytes long the lookup key was, if it found one. This can be used to advance a cursor in calling code if a lookup key was found.
Example:
false === $smilies->read_token( 'Not sure :?.', 0, $token_byte_length );
'π' === $smilies->read_token( 'Not sure :?.', 9, $token_byte_length );
2 === $token_byte_length; Example:
while ( $at < strlen( $input ) ) {
$next_at = strpos( $input, ':', $at );
if ( false === $next_at ) {
break;
}
$smily = $smilies->read_token( $input, $next_at, $token_byte_length );
if ( false === $next_at ) {
++$at;
continue;
}
$prefix = substr( $input, $at, $next_at - $at );
$at += $token_byte_length;
$output .= "{$prefix}{$smily}";
}Compatibility
- WordPress
- since 6.6.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
$textstring- String in which to search for a lookup key.
$offsetintoptional- How many bytes into the string where the lookup key ought to start. Default 0.Default:
0 $matched_token_byte_lengthoptional- Default:
null $case_sensitivitystringoptional- Pass 'ascii-case-insensitive' to ignore ASCII case when matching. Default 'case-sensitive'.Default:
'case-sensitive'
Return value
string|null- Mapped value of lookup key if found, otherwise
null.
Performance profile
How much work a call to WP_Token_Map::read_token() 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
- Light
- Scaling
- Scales with input
- Instructions
- 16β80
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 114.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What one call costs Β· 8 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Token_Map::read_token() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!$text_length | 16 | none |
$text_length | 20β45 | strcspn() |
!$text_length | 23 | ->read_small_token() |
$text_length && $case_sensitivity === "ascii-case-insensitive" | 39β49 | strcspn(), stripos() |
$text_length && $case_sensitivity !== "ascii-case-insensitive" | 42β52 | strcspn(), ->read_small_token() |
$text_length && $case_sensitivity === "ascii-case-insensitive" | 46β56 | strcspn(), stripos(), ->read_small_token() |
$text_length && $case_sensitivity !== "ascii-case-insensitive" && $group_at !== false && $at | 76 | strcspn(), unpack(), unpack(), substr_compare() |
$text_length && $case_sensitivity === "ascii-case-insensitive" && $group_at !== false && $at | 80 | strcspn(), stripos(), unpack(), unpack(), substr_compare() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 112 | 16β80 | 8 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 114 | 16β80 | 8 | |
| 8.4 | 114 | 16β80 | 8 | 13 fewer instructions than PHP 8.3 |
| 8.3 | 127 | 16β90 | 8 | |
| 8.2 | 127 | 16β90 | 8 | |
| 8.1 | 127 | 16β90 | 8 | 3 fewer instructions than PHP 7.4 |
| 7.4 | 130 | 16β92 | 8 |
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.
Uses Β· 2
- stripos()
- WP_Token_Map::read_small_token()Finds a match for a short word at the index.
Source code
public function read_token( string $text, int $offset = 0, &$matched_token_byte_length = null, $case_sensitivity = 'case-sensitive' ): ?string { $ignore_case = 'ascii-case-insensitive' === $case_sensitivity; $text_length = strlen( $text ); // Search for a long word first, if the text is long enough, and if that fails, a short one. if ( $text_length > $this->key_length ) { /* * Keys cannot contain null bytes, which is taken care of for the full words, * but here itβs required to reject group keys with null bytes so that the * lookup doesnβt get off track when scanning the group string. */ if ( strcspn( $text, "\x00", $offset, $this->key_length ) < $this->key_length ) { return null; } $group_key = substr( $text, $offset, $this->key_length ); $group_at = $ignore_case ? stripos( $this->groups, $group_key ) : strpos( $this->groups, $group_key ); if ( false === $group_at ) { // Perhaps a short word then. return strlen( $this->small_words ) > 0 ? $this->read_small_token( $text, $offset, $matched_token_byte_length, $case_sensitivity ) : null; } $group = $this->large_words[ $group_at / ( $this->key_length + 1 ) ]; $group_length = strlen( $group ); $at = 0; while ( $at < $group_length ) { $token_length = unpack( 'C', $group[ $at++ ] )[1]; $token = substr( $group, $at, $token_length ); $at += $token_length; $mapping_length = unpack( 'C', $group[ $at++ ] )[1]; $mapping_at = $at; if ( 0 === substr_compare( $text, $token, $offset + $this->key_length, $token_length, $ignore_case ) ) { $matched_token_byte_length = $this->key_length + $token_length; return substr( $group, $mapping_at, $mapping_length ); } $at = $mapping_at + $mapping_length; } } // Perhaps a short word then. return strlen( $this->small_words ) > 0 ? $this->read_small_token( $text, $offset, $matched_token_byte_length, $case_sensitivity ) : null; }Changelog
Introduced in 6.6.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/class-wp-token-map.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.