string str_pad ( string input, int pad_length [, string pad_string [, int pad_type]])
Next up, str_pad() makes a given string (parameter one) larger by X number of characters (parameter two) by adding on spaces. For example:
<?php $string = "Goodbye, Perl!"; $newstring = str_pad($string, 10); ?>That code would leave " Goodbye, Perl! " in $newstring, which is the same string from $string except with five spaces on either side, equalling the 10 we passed in as parameter two.
Str_pad() has an optional third parameter that lets you set the padding character to use, so:
<?php $string = "Goodbye, Perl!";> $newstring = str_pad($string, 10, 'a'); ?>That would put "aaaaaGoodbye, Perl!aaaaa" into $newstring.
We can extend the function even more by using it is optional fourth parameter, which allows us to specify which side we want the padding added to. The fourth parameter is specified as a constant, and you either use STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH:
<?php $string = "Goodbye, Perl!"; $a = str_pad($string, 10, '-', STR_PAD_LEFT); $b = str_pad($string, 10, '-', STR_PAD_RIGHT); $c = str_pad($string, 10, '-', STR_PAD_BOTH); ?>That code will set $a to be "----------Goodbye, Perl!", $b to be "Goodbye, Perl!----------", and $c to be "-----Goodbye, Perl!-----", as expected.
Note that HTML only allows a maximum of one space at any time. If you want to pad more, you will need to use " ", the HTML code for non-breaking space.
Want to learn PHP 7?
Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!
Next chapter: Complex string printing >>
Previous chapter: Comparing strings
Jump to:
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.
Pad a string or zero fill a number using PHP.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
$number = 5; | |
echo sprintf('%02s', $number); // 05 | |
echo sprintf('%03s', $number); // 005 | |
echo sprintf('%02s', 453); // 453 | |
// sprintf is faster than str_pad |
Created: March-04, 2020 | Updated: April-13, 2021 In PHP, numbers or integers with leading zeros can have different
values with unexpected results. Example: To make sure that the number will not lose its natural meaning, there are several ways to try like using a string instead of a number or by using different functions such as substr, printf() / sprintf() and
str_pad. The easiest approach; just simply to use string as a substitute for the number. When To Use: This method clips the numbers from the left when the string length exceeded. If the start is negative, the returned string will start from the start'th character from the end of the string. Example: When To Use:Use a String to Substitute the Number
substr() to Add Leading Zeros in PHP
- When there’s a fixed length of the output string.
- Adding zeros when the string is less than the length.
printf()/sprintf() to Add Leading Zeros in PHP
To pad an output for a fixed length, when the input is less than the length, and return the string when input is greater.
Example:
$length = 10; $char = 0; $type = 'd'; $format = "%{$char}{$length}{$type}"; // or "$010d"; //print and echo printf($format, 987654321); //store to a variable $newFormat = sprintf($format, 987654321); // output: 0987654321In the example, the fixed length is set to 10 and the input length is 9, so it adds one zero in the left if using printf()/sprintf.
sprintf() Parameter Values
format (Required) | The string and how to format the variables. Possible format values: %% - Percent sign %b - Binary %c - Character referenced to ASCII %d - decimal number (negative or positive) %e - Lowercase scientific notation %E - Uppercase scientific notation %u - Unsigned decimal number %f - Float number (local settings aware) %F - Float number (not local settings aware) %g - shorter version of %e and %f %G - shorter version of %E and %F %o - Octal %s - String %x - Hexadecimal (lowercase) %X - Hexadecimal (uppercase) |
arg1 (Required) | To be inserted at the first % sign |
arg2 (Optional) | To be inserted at the second % sign |
argg++ (Optional) | To be inserted at the third, fourth, etc. % sign |
Note:
- If the input string length is greater than or equal to the pad length, it will only return the string - no characters will be omitted.
- Padding is only added the length of the input is less than the padding length.
str_pad() to Add Leading Zeros in PHP
This method will pad a string to a new length of specified characters.
Example:
$length = 7; $string = "12345"; echo str_pad($string,$length,"0", STR_PAD_LEFT); //output: 0012345The example above will add the zero to the specified string until it matches the specified length (which is 7 in this case).
- str_pad() Parameters List
string | The string to pad |
length | Specifies the new length of the string. Note: If the value is less than the length of the string, nothing will happen. |
pad_string | Specifies the string to use for padding. The default value is whitespace. |
pad_type | Specifies where to pad the string. Accepted Values: STR_PAD_BOTH - This will pad both sides of the string. STR_PAD_LEFT - Pad the left side of the string STR_PAD_RIGHT - Pad to the right side of the string |
Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.