PHP sprintf () for formatting output

Source: Internet
Author: User

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 code

$ PostObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA );
$ FromUsername = $ postObj-> FromUserName;
$ ToUsername = $ postObj-> ToUserName;
$ Keyword = trim ($ postObj-> Content );
$ Time = time ();
$ TextTpl = "<xml>
<ToUserName> <! [CDATA [% s]> </ToUserName>
<FromUserName> <! [CDATA [% s]> </FromUserName>
CreateTime> % s </CreateTime>
<MsgType> <! [CDATA [% s]> </MsgType>
<Content> <! [CDATA [% s]> </Content>
<FuncFlag> 0 </FuncFlag>
</Xml> ";
$ 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.