Padding the End of Strings
Learn how to add padding characters to the end of a string up to a specific length.
We'll cover the following...
The padRight
helper method
We can use the padRight
helper method to add padding characters to the end of a string up to a specific length.
Press + to interact
<?php/*** Versions: Laravel 8, 9** @return string*/public static function padRight(string $value,int $length,string $pad = ' ');
This method is beneficial in ensuring a value takes up a fixed amount of space. For example, the following example will ensure that the input strings always take up at least fifteen characters:
Press + to interact
<?phpuse Illuminate\Support\Str;// Returns "Desk "Str::padRight('Desk', 15);// Returns "Lamp "Str::padRight('Lamp', 15);// Returns "Office Printer "Str::padRight('Office Printer', 15);
One use ...