Core\Str::padEnd
Core\Str::padEnd(string $s, uint $length, string $padding = " "): stringCore\Str::padEnd( string $s, uint $length, string $padding = " ", ): string
Adds text after a string until it has the length you ask for.
Core\Str::padEnd takes a string, a length and a padding. It adds copies of the padding after the string until the result has that many characters. The default padding is one space. When the padding has more than one character and does not fit a whole number of times, the last copy is cut short: padEnd("5", 4, "ab") gives "5aba".
The length counts characters as a person sees them, so é or an emoji counts as one. If the string already has the length or more, it is returned unchanged. It is never cut.
An empty padding throws a RuntimeError when the string is too short.
Core\Str::padStart adds the padding before the string instead.
Parameters
$sstringThe string to pad.$lengthuintThe length to reach, in characters as a person counts them.$paddingstringdefault " "The text to add. The last copy is cut short when only part of it fits. The default is one space.
Return value
Returns string. The padded string. If $s already has $length characters or more, it is returned unchanged.
Errors
RuntimeError$paddingis empty and$sis shorter than$length. It is also thrown when the result is larger than this process can hold.
Examples
A fixed width
<?nvs// Shows how `Core\Str::padEnd` fills a text up to a fixed width with spaces.
// "Tea" has 3 characters. This adds 3 spaces and prints `[Tea ]`.echo '[', Core\Str::padEnd('Tea', 6), "]\n";
// "Chocolate" already has more than 6 characters. This prints `[Chocolate]`.echo '[', Core\Str::padEnd('Chocolate', 6), "]\n";[Tea ][Chocolate]Dots to a page number
<?nvs// Shows how to fill a line with dots, as a table of contents does.
array<string> $chapters = ['Introduction' => '1', 'Über uns' => '14', 'Index' => '52'];
// The padding is `.`. `Ü` counts as one character, so every line has the// same width before its page number.foreach ($chapters as string $title => string $page) { echo Core\Str::padEnd($title . ' ', 20, '.'), ' ', $page, "\n";}Introduction ....... 1Über uns ........... 14Index .............. 52A help screen
<?nvs// Shows how a command-line program prints its list of commands, with every// description starting in the same column.
array<string> $commands = [ 'run' => 'Runs a program.', 'test' => 'Runs the tests of a project.', 'fmt' => 'Formats the source files.',];
echo "Commands:\n";foreach ($commands as string $name => string $description) { echo ' ', Core\Str::padEnd($name, 8), $description, "\n";}Commands: run Runs a program. test Runs the tests of a project. fmt Formats the source files.