string str_pad ( string input, int pad_length [, string pad_string [, int pad_type]]) Show
Next up, str_pad() makes a given string (parameter one) larger by X number of characters (parameter two) by adding on spaces. For example:
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:
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:
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
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 Use a String to Substitute the NumberThe easiest approach; just simply to use string as a substitute for the number.
When To Use:
substr() to Add Leading Zeros in PHPThis method clips the numbers from the left when the string length exceeded. If the Example:
When To Use:
printf()/sprintf() to Add Leading Zeros in PHPTo pad an output for a fixed length, when the input is less than the length, and return the string when input is greater. Example:
In the example, the fixed length is set to 10 and the input length is 9, so it adds one
Note:
str_pad() to Add Leading Zeros in PHPThis method will pad a string to a new length of specified characters. Example:
The
example above will add the
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. Related Article - PHP FunctionWhat is str_ pad in php?The str_pad() function is a built-in function in PHP and is used to pad a string to a given length. We can pad the input string by any other string up to a specified length. If we do not pass the other string to the str_pad() function then the input string will be padded by spaces.
Which of the following function pads one string with another?MySQL LPAD() function
MySQL LPAD() left pads a string with another string. The actual string, a number indicating the length of the padding in characters (optional) and the string to be used for left padding - all are passed as arguments.
How do you repeat a string to a specific number of time in PHP?The str_repeat() function repeats a string a specified number of times.
Which of the following function pads one string with another PHP?The str_pad() function pads a string to a new length.
|