php The str_pad() function is used to fill a string to a specified length. The syntax is "str_pad(string,length,pad_string,pad_type)" and returns the filled string; it can be filled with any other string Enter a string until it reaches the specified length.
#php How to use str_pad function?
php str_pad function can pad the string to the specified length
Syntax:
str_pad(string,length,pad_string,pad_type)
Parameters:
● String is required, specifies the string to be filled,
● length is required, specifies the new string length, if the value is less than the original length of the string, then No action is taken.
● pad_string is optional and specifies the string used for padding. The default is blank.
● pad_type is optional and specifies which side of the string to fill. STR_PAD_BOTH -- fills both sides of the string. If it is not an even number, the right side gets extra padding. STR_PAD_LEFT – pad the left side of the string. STR_PAD_RIGHT -- pad the right side of the string, default.
Description: We can fill the input string with any other string until the specified length is reached. If we do not pass another string to the str_pad() function, then the input string will be padded with spaces.
Example:
<?php $i = "hello world"; $j = str_pad($i,30,"!"); //未设置填充字符串位置,默认为右侧 echo $j; echo "<br>"; $k = "hello world"; $h = str_pad($k,30,".",STR_PAD_LEFT);//设置填充字符串的位置为左侧 echo $h; ?>
Output:
hello world!!!!!!!!!!!!!!!!!!! ...................hello world
The above is the detailed content of How to use php str_pad function?. For more information, please follow other related articles on the PHP Chinese website!