Laravel String Wordwrap


August 23rd, 2023

Laravel String Wordwrap

The newly introduced wordWrap method in the Laravel String API effectively splits strings within a string based on a specified character limit. This serves as a wrapper for PHP's native wordwrap function and is designed to provide Laravel developers with more flexibility in handling strings.

Below, you'll find the syntax of how to use this new method:

Using the String Facade

use Illuminate\Support\Str;
$text = "A very long woooooooooooord."
Str::wordWrap(string: $text, characters: 8);
A very

Using Stringable

str($text)->wordWrap(string: $text, characters: 8);

These examples demonstrate how easy it is to break up a string into multiple lines, keeping a specified character limit on each line.

Filed in:

Eric L. Barnes

Eric is the creator of Laravel News and has been covering Laravel since 2012.