Home > Article > Backend Development > How to solve the problem of insufficient length of php
Solution to the insufficient length of php: 1. Create a PHP sample file; 2. Use the "str_pad($value,2,0,STR_PAD_LEFT);" method to realize that the fixed length of the string is not enough to supplement other strings. Can.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
How to solve the problem of insufficient php length?
php The fixed length of the string is not enough to supplement other strings
// str_pad(带填补的字符串,填补后的长度,填补字符串,填补位置) $result=["7","6","8","4","3","23","27","9","5","19","10","21"]; if($result){ foreach ($result as $key => $value){ $result[$key]=str_pad($value,2,0,STR_PAD_LEFT); } //$result =["07","06","08","04","03","23","27","09","05","19","10","21"]
Related introduction:
str_pad() function fills the string with the new length.
Syntax
str_pad(string,length,pad_string,pad_type)
Parameters
string必需。规定要填充的字符串。 length必需。规定新的字符串长度。如果该值小于字符串的原始长度,则不进行任何操作。 pad_string可选。规定供填充使用的字符串。默认是空白。 pad_type
Optional. Specifies which side of the string to fill.
Possible values:
STR_PAD_BOTH - 填充字符串的两侧。如果不是偶数,则右侧获得额外的填充。 STR_PAD_LEFT - 填充字符串的左侧。 STR_PAD_RIGHT - 填充字符串的右侧。默认。
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to solve the problem of insufficient length of php. For more information, please follow other related articles on the PHP Chinese website!