_split_str_by_whitespace( string $text, int $goal ): array
- Since
- 3.4.0
- Source
wp-includes/formatting.php:3253
Description
The length of each returned chunk is as close to the specified length goal as possible, with the caveat that each chunk includes its trailing delimiter.
Chunks longer than the goal are guaranteed to not have any inner whitespace.
Joining the returned chunks with empty delimiters reconstructs the input string losslessly.
Input string must have no null characters (or eventual transformations on output chunks must not care about null characters)
_split_str_by_whitespace( "1234 67890 1234 67890a cd 1234 890 123456789 1234567890a 45678 1 3 5 7 90 ", 10 ) ==
array (
0 => '1234 67890 ', // 11 characters: Perfect split.
1 => '1234 ', // 5 characters: '1234 67890a' was too long.
2 => '67890a cd ', // 10 characters: '67890a cd 1234' was too long.
3 => '1234 890 ', // 11 characters: Perfect split.
4 => '123456789 ', // 10 characters: '123456789 1234567890a' was too long.
5 => '1234567890a ', // 12 characters: Too long, but no inner whitespace on which to split.
6 => ' 45678 ', // 11 characters: Perfect split.
7 => '1 3 5 7 90 ', // 11 characters: End of $text.
);Compatibility
- WordPress
- since 3.4.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- The string to split.
$goalint- The desired chunk length.
Return value
array- Numeric array of chunks.
Performance profile
How much work a call to _split_str_by_whitespace() 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
- 11–27
- Plugin surface
- None
- Called by
- 1
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 38.
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 one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost _split_str_by_whitespace() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!$goal | 11–13 | none |
$goal && $pos === false | 25–27 | strrpos() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 38 | 11–27 | 4 | |
| 8.5 | 38 | 11–27 | 4 | |
| 8.4 | 38 | 11–27 | 4 | 19 fewer instructions than PHP 8.3 |
| 8.3 | 57 | 15–37 | 4 | |
| 8.2 | 57 | 15–37 | 4 | |
| 8.1 | 57 | 15–37 | 4 | |
| 7.4 | 57 | 15–37 | 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
- make_clickable()Converts plaintext URI to HTML links.
Source code
function _split_str_by_whitespace( $text, $goal ) { $chunks = array(); $string_nullspace = strtr( $text, "\r\n\t\v\f ", "\000\000\000\000\000\000" ); while ( $goal < strlen( $string_nullspace ) ) { $pos = strrpos( substr( $string_nullspace, 0, $goal + 1 ), "\000" ); if ( false === $pos ) { $pos = strpos( $string_nullspace, "\000", $goal + 1 ); if ( false === $pos ) { break; } } $chunks[] = substr( $text, 0, $pos + 1 ); $text = substr( $text, $pos + 1 ); $string_nullspace = substr( $string_nullspace, $pos + 1 ); } if ( $text ) { $chunks[] = $text; } return $chunks;}Changelog
Introduced in 3.4.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/formatting.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.