str_pad
(PHP 4 >= 4.0.1, PHP 5, PHP 7)
str_pad — Pad a string to a certain length with another string
Description
$input
, int $pad_length
[, string $pad_string
= " "
[, int $pad_type
= STR_PAD_RIGHT
]] ) : string
This function returns the input
string
padded on the left, the right, or both sides to the specified
padding length. If the optional argument
pad_string
is not supplied, the
input
is padded with spaces, otherwise it
is padded with characters from pad_string
up to the limit.
Parameters
-
input
-
The input string.
-
pad_length
-
If the value of
pad_length
is negative, less than, or equal to the length of the input string, no padding takes place, andinput
will be returned. -
pad_string
-
Note:
The
pad_string
may be truncated if the required number of padding characters can't be evenly divided by thepad_string
's length. -
pad_type
-
Optional argument
pad_type
can beSTR_PAD_RIGHT
,STR_PAD_LEFT
, orSTR_PAD_BOTH
. Ifpad_type
is not specified it is assumed to beSTR_PAD_RIGHT
.
Examples
Example #1 str_pad() example
<?php
$input = "Alien";
echo str_pad($input, 10); // produces "Alien "
echo str_pad($input, 10, "-=", STR_PAD_LEFT); // produces "-=-=-Alien"
echo str_pad($input, 10, "_", STR_PAD_BOTH); // produces "__Alien___"
echo str_pad($input, 6, "___"); // produces "Alien_"
echo str_pad($input, 3, "*"); // produces "Alien"
?>
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-str-pad.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.