Core\Str::wrap
Splits a long text into lines of a fixed width.
Core\Str::wrap takes a text and a width, and returns the text with line breaks in it. It breaks a line at a space, and the break replaces that space. No line is longer than the width, unless one word alone is longer. The width counts characters, so "é" counts as one, the same as "e".
The break is "\n" unless you give another one with breakWith, such as "<br>". Where the text already contains the break, a new line starts there.
A word that is longer than the width stays whole on a line of its own. With cutLongWords: true, the word is cut into pieces of the width instead.
Parameters
$sstringThe text to wrap.$widthuintThe longest line, in characters. With0, every space is a line break.
Options
breakWithstringoptionalThe text written at each line break. The default is"\n". Where$salready contains this text, a new line starts there.cutLongWordsbooloptionalWithtrue, a word longer than$widthis cut into pieces of$widthcharacters. Withfalse, the default, a long word stays whole on a line of its own.
Return value
Returns string. The wrapped text. A text of at most $width characters is returned unchanged. A result over the memory limit stops the request.
Errors
RuntimeErrorbreakWithis"", or$widthis0andcutLongWordsistrue.
Examples
Wrap a paragraph
<?nvs// Shows how to split a paragraph into lines of at most 20 characters.
string $text = 'The café on the corner opens at seven and closes when the last guest leaves.';
// `Core\Str::wrap` puts a line break at the last space that fits.// This prints five lines, and none of them is longer than 20 characters.echo Core\Str::wrap($text, 20), "\n";The café on thecorner opens atseven and closeswhen the last guestleaves.Cut a long word
<?nvs// Shows what happens to a word that is longer than the width.
string $text = 'Your reference is 7F3A9C2E81B4D6F0 for this order.';
// By default, the long word stays whole on a line of its own.// Here that line has 16 characters, more than the width of 10.echo Core\Str::wrap($text, 10), "\n\n";
// With `cutLongWords: true`, the word is cut into pieces of 10 characters.echo Core\Str::wrap($text, 10, { cutLongWords: true }), "\n";Yourreferenceis7F3A9C2E81B4D6F0for thisorder.
Yourreferenceis7F3A9C2E81B4D6F0 forthisorder.A comment in a web page
<?nvs// Shows how to break a comment into short lines for a web page.
string $comment = 'Great service, fast delivery and the package was very well protected. Will order again!';
// `breakWith` sets the text that is written at each line break.// Here it is `<br>`, so a browser shows each part on its own line.echo Core\Str::wrap($comment, 30, { breakWith: '<br>' }), "\n";
// The text already has one `<br>`. A new line starts there,// so "Thanks!" is not joined to the line after it.echo Core\Str::wrap('Thanks!<br>The size was right and the colour is lovely.', 20, { breakWith: '<br>' }), "\n";Great service, fast delivery<br>and the package was very well<br>protected. Will order again!Thanks!<br>The size was right<br>and the colour is<br>lovely.