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 verylongwoooooooooooord.*/
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.
Eric is the creator of Laravel News and has been covering Laravel since 2012.