Str_pad
- (PHP 4 >= 4.0.1, PHP 5, PHP 7)
- Str_pad-pad a string to a certain length with another string
- Str_pad-fills a string with another string for a specified length
Description
str_pad( $input, $pad_length[, $pad_string" "[, $pad_typeSTR_PAD_RIGHT]])//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.//该函数返回 input 被从左端、右端或者同时两端被填充到制定长度后的结果。如果可选的 pad_string 参数没有被指定,input 将被空格字符填充,否则它将被 pad_string 填充到指定长度。
Parametersinput
- The input string.
- 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, and Input would be returned.
- If the value of pad_length is negative, less than or equal to the length of the input string, no padding occurs, and input is returned.
Pad_string
Note:
- The pad_string may truncated if the required number of padding characters can ' t is evenly divided by the pad_string ' s L Ength.
- If the length of the padding character cannot be divisible by pad_string, then pad_string may be shortened.
Pad_type
- Optional argument Pad_type can be str_pad_right, str_pad_left, or Str_pad_both. If Pad_type is not specified it's assumed to be str_pad_right.
- The possible values for the optional pad_type parameter are
STR_PAD_RIGHT
, STR_PAD_LEFT
or STR_PAD_BOTH
. If Pad_type is not specified, it is assumed to be STR_PAD_RIGHT
.
Return Values
- Returns the padded string.
- Returns the populated string.
Examples
<?php/** * Created by Phpstorm. * User:zhangrongxiang * DATE:2018/2/19 * Time: PM 10:59 *///function str_pad ($input, $pad _length, $pad _string = null, $p Ad_type = null) {} $input = "zing"; $str = Str_pad ($input);//"Zing" echo $str. Php_eol;//10echo strlen ($STR). Php_eol;//zing******echo Str_pad ($input, 10, "*"). php_eol;//^^ ^^ ^^ zingecho str_pad ($input, "^", str_pad_left). Php_eol; Output "-=-=-alien"//___zing___echo str_pad ($input, Ten, "_", Str_pad_both). Php_eol;//zing=echo Str_pad ($input, 5, "= ="). Php_eol;//zingecho Str_pad ($input, 3, "-"). php_eol;///////////////////////////////////////////////////////////////** * Since the default pad_type is STR_PAD_ Right. * Using Str_pad_both were always favor in the right PAD * If the required number of padding characters can ' t be evenly div IDed. *///ppinputpppecho str_pad ("Input", Ten, "pp", Str_pad_both). Php_eol;//inputpecho str_pad ("Input", 6, "P", Str_pad_both). Php_eol;//pinputppecho Str_pad ("inPut ", 8," P ", Str_pad_both). Php_eol;/////////////////////////////////////////////////////////////function Str_pad_unicode ($str, $pad _len, $ Pad_str = ", $dir = str_pad_right) {$str _len = Mb_strlen ($STR); $pad _str_len = Mb_strlen ($pad _str); if (! $str _len && ($dir = = Str_pad_right | | $dir = = str_pad_left)) {$str _len = 1;//@debug} if (! $pad _len | |! $pad _str_len | | $pad _len <= $str _len) {return $str; } $result = null; $repeat = ceil ($str _len-$pad _str_len + $pad _len); if ($dir = = str_pad_right) {$result = $str. Str_repeat ($pad _str, $repeat); $result = mb_substr ($result, 0, $pad _len); } else if ($dir = = str_pad_left) {$result = Str_repeat ($pad _str, $repeat). $str; $result = Mb_substr ($result,-$pad _len); } else if ($dir = = Str_pad_both) {$length = ($pad _len-$str _len)/2; $repeat = Ceil ($length/$pad _str_len); $result = Mb_substr (Str_repeat ($pad _str, $repeat), 0, Floor ($length)). $str. Mb_substr (Str_repeat ($pad _str, $repeat), 0, Ceil ($length)); } return $result;} $str = "Shoot the yellow film";//ha-pat-huang-ha-echo str_pad_unicode ($STR, 5, ' ha ', Str_pad_both). php_eol;//hahaha Shoot the yellow piece haha haha echo str_pad_unicode ($str, 10, ' ha ', Str_pad_both). Php_eol;function Mb_str_pad ($str, $pad _len, $pad _str = ", $dir = str_pad_right, $encoding = null) {$encoding = $encoding = = = null? Mb_internal_encoding (): $encoding; $padBefore = $dir = = = Str_pad_both | | $dir = = = Str_pad_left; $padAfter = $dir = = = Str_pad_both | | $dir = = = Str_pad_right; $pad _len-= Mb_strlen ($str, $encoding); $targetLen = $padBefore && $padAfter? $pad _LEN/2: $pad _len; $strToRepeatLen = Mb_strlen ($pad _str, $encoding); $repeatTimes = Ceil ($targetLen/$strToRepeatLen); $repeatedString = Str_repeat ($pad _str, max (0, $repeatTimes)); Safe if used WITH valid utf-8 strings $before = $padBefore? Mb_substr ($repeatedString, 0, Floor ($targetLen), $encoding): "; $after = $padAfter? Mb_substr ($repeatedString, 0, Ceil ($targetLen), $encoding): "; Return $before. $str. $after;} Ha-pat-huang-ha-echo mb_str_pad ($STR, 5, ' ha ', Str_pad_both). php_eol;//hahaha Shoot the yellow piece haha haha echo mb_str_pad ($str, 10, ' ha ', Str_pad_both). Php_eol;///////////////////////////////////////////////////////////////////////function Pad_between_strings ($ String1, $string 2, $length, $char = "") {$fill _length = $length-(strlen ($string 1) + strlen ($string 2)); Return $string 1. Str_repeat ($char, $fill _length). $string 2;} Abc**************123echo pad_between_strings ("abc", "123", 20, "*"). Php_eol;
See
- http://php.net/manual/zh/function.str_pad.php
All rights reserved
The Str_pad () function of the PHP string is used