Master PHP’s str_pad(): Padding, Alignment, Truncation & Formatting Explained

This article provides a comprehensive guide to PHP’s str_pad() function, covering its syntax, parameters, return values, and practical examples such as string padding, alignment, truncation, and numeric formatting, enabling developers to manipulate string lengths efficiently.

php Courses
php Courses
php Courses
Master PHP’s str_pad(): Padding, Alignment, Truncation & Formatting Explained
str_pad()

is a PHP function for padding strings, allowing characters to be added to the left, right, or both sides to reach a specified length.

Basic syntax of str_pad()

str_pad(string $input, int $pad_length, string $pad_string [, int $pad_type = STR_PAD_RIGHT]): string

Parameter description:

$input

: the string to be padded. $pad_length: the length of the resulting string. $pad_string: the padding characters. $pad_type: padding type, can be STR_PAD_LEFT (left), STR_PAD_RIGHT (right), or STR_PAD_BOTH (both); default is STR_PAD_RIGHT.

Return value:

Returns the padded string.

Example:

Below is a simple example demonstrating how to use str_pad() to add padding characters to the left side of a string:

$input = "Hello";
$pad_length = 10;
$pad_string = "-";
$pad_type = STR_PAD_LEFT;
$result = str_pad($input, $pad_length, $pad_string, $pad_type);
echo $result;

Output:

-----Hello

Usage of str_pad()

Beyond basic padding, str_pad() can be combined with other string functions for more complex tasks.

1. String alignment

str_pad()

can be used for left, right, and center alignment. Example:

$text = "Hello";
$length = 10;
$pad_string = " ";
$leftAligned = str_pad($text, $length, $pad_string, STR_PAD_RIGHT);
$rightAligned = str_pad($text, $length, $pad_string, STR_PAD_LEFT);
$centerAligned = str_pad($text, $length, $pad_string, STR_PAD_BOTH);
echo $leftAligned . "
";
echo $rightAligned . "
";
echo $centerAligned . "
";

Output:

Hello     
    Hello
  Hello

2. String truncation

str_pad()

can be used together with substr() to truncate strings. Example:

$text = "Hello World";
$length = 10;
$pad_string = " ";
$paddedText = str_pad($text, $length, $pad_string);
$truncatedText = substr($paddedText, 0, $length);
echo $truncatedText;

Output:

Hello Worl

3. Formatting strings

str_pad()

can format numeric strings by adding leading zeros. Example:

$number = "123";
$length = 6;
$pad_string = "0";
$formattedNumber = str_pad($number, $length, $pad_string, STR_PAD_LEFT);
echo $formattedNumber;

Output:

000123

Summary

This article detailed the basic usage, parameters, and examples of PHP’s str_pad() function. It demonstrated how the function can be used for padding, alignment, truncation, and formatting, enhancing string handling flexibility and efficiency in PHP development.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPCode Examplesstring paddingString Manipulationstr_pad
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.