Strings\wordWrap()

transformer returns Closure pure
(int, string, bool) → (string → string)
At a glance — Wraps wordwrap(). Bind width, break, and whether to hard-cut long words.

Allows you to create a function which can be used to break a string with a maximum width, respecting word boundaries. These can either be used as part of a Higher Order Function such as array_map() or as part of a compiled/pipe function.

/**
  * @param int $width Max width for each "line"
  * @param string $break The string to use to denote the end of line.
  * @param bool $cut If set to true, words are cut at $width, else overflow.
  * @return Closure(string):string
  * @psalm-pure
  */
 Strings\wordWrap(int $width, string $break = "\n", bool $cut = false): Closure

Returned Closure

When Strings\wordWrap() is called, it returns the following Closure which can be used like a regular function.

/**
  * @param string $string The string to be chunked
  * @return string
  * @psalm-pure
  */ 
$function(string $string): string

Examples

Partial Application

This can be used to create a simple closure which can be used as a regular function.

// Create a closure which will chunk a string at 8 characters, with a - as the end, splitting words.
$wordWrap8DashSplit = Strings\wordWrap(8, '-', true);

// Create a closure which will chunk a string at 8 characters, with a - as the end, NOT splitting words.
$wordWrap8DashWithout = Strings\wordWrap(8, '-', false);

// Called as a function.
echo $wordWrap8DashSplit('Today I dressed my unicorn in preparation for the race.');
// Today I-dressed-my-unicorn-in-preparat-ion for-the-race.

echo $wordWrap8DashWithout('Today I dressed my unicorn in preparation for the race.');
// Today I-dressed-my-unicorn-in-preparation-for the-race.

// Used in a higher order function.
$array = array_map( $wordWrap8DashSplit, [
 'Today I dressed my unicorn in preparation for the race.', 
 'Producing random sentences can be helpful in a number of different ways.'
]);
print_r($array); /* [
  Today I-dressed-my-unicorn-in-preparat-ion for-the-race,
  Producin-g random-sentence-s can be-helpful-in a-number-of-differen-t ways.
 ] */

Curried

This can be called inline using currying.

echo Strings\wordWrap('5', '*', true)('It is such a wonderful day'); // It is*such*a*wonde*rful*day
echo Strings\wordWrap('5', '*', false)('It is such a wonderful day'); // It is*such*a*wonderful*day
// false is the cut default, so can be called as Strings\wordWrap('5', '*')

Inlined with Higher Order Function

If you are not planning on reusing the Closure created, you can just call it inline with a higher order function as its callable.

$array = array_map( Strings\wordWrap(8, '-', true), [
 'Today I dressed my unicorn in preparation for the race.', 
 'Producing random sentences can be helpful in a number of different ways.'
]);
print_r($array); /* [
  Today I-dressed-my-unicorn-in-preparat-ion for-the-race,
  Producin-g random-sentence-s can be-helpful-in a-number-of-differen-t ways.
 ] */

Details

Strings Functions

Releated String manipulation Functions