Search⌘ K
AI Features

Padding Both Sides of a String

Understand how to use Laravel's padBoth method to add padding characters to both sides of strings, enabling you to create centered headers and aligned text formatting within your PHP applications. This lesson guides you through practical examples using Laravel's fluent string class.

We'll cover the following...

The padBoth helper method

In this lesson, we will look at the padBoth helper method, which adds a padding character to both ends of a string up to a certain length.

PHP
<?php
/**
* Versions: Laravel 8, 9
*
* @return string
*/
public static function padBoth(
string $value,
int $length,
string $pad = ' '
);

The following examples demonstrate the basic usage of the padBoth helper method:

PHP
<?php
use Illuminate\Support\Str;
// Returns "Hello"
Str::padBoth('Hello', 15);
// Returns "*****World*****"
Str::padBoth('World', 15, '*');

We can use the padBoth helper method to ...