Php number or string completion PHP string automatic filling, automatic completion .? Method 1: define and use the sprintf () function to Write formatted strings into a variable .? Syntax sprintf (format, arg1, arg2, arg ++ )? Php numeric or string completion parameters
PHP string auto-filling and auto-completion.
?
Method 1:
Definition and usage
The sprintf () function writes formatted strings to a variable.
?
Syntax
sprintf(format,arg1,arg2,arg++)
?
Parameter description
Format |
Required. Conversion format. |
Arg1 |
Required. Specifies the parameter inserted at the first % symbol in the format string. |
Arg2 |
Optional. Specifies the parameter inserted at the second % symbol in the format string. |
Arg ++ |
Optional. Specifies the parameters inserted to the third, fourth, and other % symbols in the format string. |
Description
ParametersFormatIs the conversion format, starting from the percent sign ("%") to the end of the conversion character. PossibleFormatValue:
- %-Percentage sign returned
- % B-binary number
- % C-characters based on ASCII values
- % D-signed decimal number
- % E-resumable counting (for example, 1.5e + 3)
- % U-unsigned decimal number
- % F-floating point number (local settings aware)
- % F-floating point number (not local settings aware)
- % O-octal values
- % S-string
- % X-hexadecimal (lowercase letter)
- % X-hexadecimal (uppercase letters)
Parameters such as arg1, arg2, ++ are inserted to the percent sign (%) in the main string. This function is executed step by step. In the first % symbol, insert arg1, at the second % symbol, insert arg2, and so on.
?
?
With no decimals: %1\$u",$number);echo $txt;//With 2 decimals: 123.00 //With no decimals: 123?>
?
?
"% 02 s"Indicates the output is a string or number with a length of 2. if the length is insufficient, the left side is supplemented with zero. if it is written as"% 2 sBy default, space is used for completion. if you want to use other characters for completion, you must add a single quotation mark before the character, that is, in the form of"% '#2 s"Indicates completion by the well number. Finally, if you want completion to happen on the right side of the string, add the minus sign after the percent sign."%-02 s".
?
Source: http://www.w3school.com.cn/php/func_string_sprintf.asp
?
Method 2:
Definition and usage
The str_pad () function fills the string with the specified length.
?
Syntax
str_pad(string,length,pad_string,pad_type)
?
Parameter description
String |
Required. Specifies the string to be filled. |
Length |
Required. Specify the length of the new string. If the value is smaller than the length of the original string, no operation is performed. |
Pad_string |
Optional. The specified string for filling. The default value is blank. |
Pad_type |
Optional. Specify the other side of the string to be filled. Possible values:
- STR_PAD_BOTH-fill in the two ends of the string. If it is not an even number, the right side will receive additional filling.
- STR_PAD_LEFT-fill in the left side of the string.
- STR_PAD_RIGHT-fill in the string to the right. This is the default one.
|
?
?
?
?
?
Source: http://www.w3school.com.cn/php/func_string_str_pad.asp
?
?
Method 3: incomplete digits only
?
?
?
?
The source of the post: http://justcoding.iteye.com/blog/998424
?
?
?