wppaste
WordPress

_split_str_by_whitespace( string $text, int $goal ): array

Since
3.4.0
Source
wp-includes/formatting.php:3182
Breaks a string into chunks by splitting at whitespace characters.

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

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
11–27

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

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 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.

WhenInstructionsCalls it makes
!$goal11–13none
$goal && $pos === false25–27strrpos()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev3811–274
8.53811–274
8.43811–27419 fewer instructions than PHP 8.3
8.35715–374
8.25715–374
8.15715–374
7.45715–374

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 _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.

  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/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.