PHPsprintf () format the output. The sprintf () function is very useful in many places, that is, I recently wrote an automatic reply interface, where there is such a piece of code. The code is as follows: Copy the code $ postObjsi sprintf () function is very useful in many places, that is, I recently wrote an automatic reply interface, where there is such a piece of code.
The code is as follows: |
|
$ PostObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA ); $ FromUsername = $ postObj-> FromUserName; $ ToUsername = $ postObj-> ToUserName; $ Keyword = trim ($ postObj-> Content ); $ Time = time (); $ TextTpl =" %s %s CreateTime> % s %s %s 0 "; $ ResultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr ); Echo $ resultStr; |
Maybe many of my friends don't know why to write it like this. what does % S after this write mean? next I will go to the topic.
Syntax
String sprintf (string $ format [, mixed $ args [, mixed $...])
Parameter description
Format is required. Conversion format.
Args is optional. Specifies the parameter inserted at the % symbol in the format string.
Description
The format string is composed of zero or more commands: common characters (except %) will be directly copied to the result, conversion indicator, each result takes its own parameter. this applies to sprintf () and printf ().
The format parameter is the conversion format. it starts with the percent sign ("%") and ends with the conversion character. The following possible format values:
1. an optional symbol indicator forces a symbol (-or +) to be used on a number. by default, only the-symbol is used on a number if it is a negative value. This indicator also forces a + symbol to be appended to a positive number.
2. an optional fill indicator is used to fill the result to the correct string length. this may be a blank character or a 0 (zero character ). blank by default. you can specify a replacement fill character by adding a single quotation mark prefix (').
3. an optional alignment indicator indicates that the result should be left-aligned or right-aligned. the default alignment is right. a-character here will align it left.
4. an optional number. a width indicator indicates the number (minimum) of characters that a conversion may result in ).
5. an optional precision indicator is a period ('. ') followed by an optional decimal number string, that is, the number of decimal digits should be displayed as a floating point number. When this indicator is used in a string as a breakpoint, set a maximum character limit to the string.
6. a type indicator indicates the type of the parameter data. Possible types:
%-Return percent sign
B-binary number
C-characters based on ASCII values
D-signed decimal number
E-scientific notation (for example, 1.5e + 3)
E-scientific notation (for example, 1.2E + 2). (uppercase letters)
U-unsigned decimal number
F-floating point number (local settings aware)
F-floating point number (not local settings aware)
G-shorter of % e and % f.
G-shorter of % E and % f.
O-octal values
S-string
X-hexadecimal (lowercase letter)
X-hexadecimal number (uppercase letters)
Parameters such as arg1, arg2, and agr ++ 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.
Http://www.bkjia.com/PHPjc/628973.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/628973.htmlTechArticlesprintf () functions are very useful in many places, that is, I recently wrote an automatic reply interface, which has such a piece of code. The code is as follows $ postObj = si...