PHP sprintf() implements formatted output_PHP tutorial

WBOY
Release: 2016-07-13 17:14:26
Original
1674 people have browsed it

The sprintf() function is of great use in many places. I recently wrote a WeChat automatic reply interface, which contains this piece of code.

5. An optional precision indicator is in the form of a period ('.') followed by an optional string of decimal digits, that is, how many decimal digits should be displayed as floating point numbers . When this indicator is used within a string it acts as a break point, setting a maximum character limit for the string.
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 = "
 
 
 CreateTime>%s
 
 
 0
 
";
 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
 echo $resultStr;

Copy code

$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
                   $keyword = trim($postObj->Content);
                    $time = time();

                        $textTpl = "



CreateTime>%s

0

";

$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);

echo $resultStr;

Many friends may not know why they write it like this after reading it. What does the %S behind it mean? I will get into the topic below

Grammar

string sprintf ( string $format [, mixed $args [, mixed $... ]] )

Parameter Description

format required. Convert format.
args optional. Specifies parameters to be inserted into the format string at % symbols.

Description

The argument format string is composed of zero or more directives: normal characters (except %) will be copied directly to the result, conversion indicators, each result takes its own argument. This applies to sprintf() and printf() .

The format parameter is the format of the conversion, starting with the percent sign ("%") and ending with the conversion character. Possible format values ​​below:

1. An optional sign indicator forces a sign (- or +) to be used on a number. By default, only the – sign is used on a number if it is a negative value. This indicator also forces positive numbers to be appended with a + sign.

2. An optional padding indicator is what character will be used to pad the result to the correct string length. This may be a blank character or a zero character. Blanks are padded by default. A replacement can be specified Fill characters are prefixed by a single quote (').

3. An optional alignment indicator says whether the result should be left-aligned or right-aligned. The default is right-aligned; a - character here will make it left-aligned.

4. An optional number, a width indicator that says how many characters (minimum) this conversion should result in.

6. A type indicator says what type the parameter data should be treated as. Possible types: % – Returns the percent symbol b – Binary number c – Character according to ASCII value d – signed decimal number e – scientific notation (e.g. 1.5e+3) E – scientific notation (e.g. 1.2E+2). (capital letter) 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 number s – string x – hexadecimal number (lowercase letter) X – Hexadecimal number (capital letter) Arguments arg1, arg2, agr++, etc. are inserted into the main string at the percent sign (%) symbol. This function is executed step by step. At the first % sign, arg1 is inserted, at the second % sign, arg2, and so on.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628973.htmlTechArticleThe sprintf() function is very useful in many places. It is a WeChat automatic reply interface I wrote recently. , which contains such a piece of code. The code is as follows Copy the code $postObj = si...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!