PHP sprintf () format the output of the detailed ____php

Source: Internet
Author: User
Tags lowercase sprintf uppercase letter
I. Definition and usage

The sprintf () function writes the formatted string to the variable.

The Arg1, arg2, + + parameters are inserted into the main string at the percent sign (%) symbol. The function is executed incrementally. At the first% symbol, insert arg1, insert arg2 at the second% symbol, and so on.

Note: If the% symbol is greater than the ARG argument, you must use a placeholder. The placeholder is after the% symbol and consists of numbers and "\$." Example: <. PHP $number = 2; $STR = "Shanghai"; $txt = sprintf ("There are%u million cars in%s.", $number, $STR); Echo $txt?> Run the result is: There are 2 million cars in Shanghai. Grammar sprintf (format,arg1,arg2,arg++)

Parameters Describe
Format Necessary. Specify the string and how to format the variables therein.
Possible format values:
Percent% return a percent percent
%b binary number
Character for%c ASCII value
%d contains positive and negative decimal digits (negative, 0, positive numbers)
%e uses the scientific notation of lowercase (for example: 1.2e+2)
%E uses the scientific notation of uppercase (for example: 1.2E+2)
%u does not contain a positive or negative decimal number (greater than or equal to 0)
%f floating-point numbers (Local settings)
%F floating-point number (not local setting)
%g Shorter%e and%f
%G Shorter%e and%f
%o Octal number
%s string
%x hex Number (lowercase letter)
%x Hex Number (uppercase letter)

The attached format value. Must be placed between% and letters (e.g.%.2f):

+ (precede the number with + or-to define the positive and negative of the number.) By default,
Only negative numbers are marked, positive numbers are not marked.
' (Specify what to use as padding, default is a space.) It must be with the width of the specified device
Used together. For example:% ' x20s (using ' x as padding ')
-(left to adjust the value of the variable)
[0-9] (Specify the minimum width of the variable value)
. [0-9] (Specify number of decimal digits or maximum string length)

Note: If you use more than one of the above format values, they must be used in the order listed above.
Arg1 Necessary. Specify the parameter to be inserted into the first% symbol in the format string.
Arg2 Optional. Specify the parameter to be inserted into the second% symbol in the format string.
arg++ Optional. Specify the parameters to be inserted into the format string at the third to fourth% symbol.






































Technical details:
Return value: Returns a formatted string Ii. More examples

1. Use format value%f:

<?php

$number = 123;

$txt = sprintf ("%f", $number);

Echo $txt;

?>


The results of the operation are: 123.000000


2. Use the occupancy character:

<?php

$number = 123;

$txt = sprintf ("With two decimal:%\$.2f<br> without decimals:%1\ $u", $number);

Echo $txt;

?>


Run as: With two decimal: 123.00

Without decimal: 123


3. Demo of all possible format values


<?php

$num 1 = 123456789;

$num 2 =-123456789;

$char = 50; ASCII character 50 is 2


Note: The format value "%" returns the percent sign

Echo sprintf ("%%b =%b", $num 1). "  <br> "; Binary number result:%b = 111010110111100110100010101


Echo sprintf ("%%c =%c", $char). "  <br> "; ASCII character result:%c = 2


Echo sprintf ("%%d =%d", $num 1). "  <br> "; Signed decimal number result:%d = 123456789


Echo sprintf ("%%d =%d", $num 2). "  <br> "; Signed decimal number result:%d =-123456789


Echo sprintf ("%%e =%e", $num 1). "  <br> "; Scientific notation (lowercase) results:%e = 1.234568e+8


Echo sprintf ("%%e =%E", $num 1). " <br> "//Scientific counting method (upper case) Result:%E = 1.234568E+8


Echo sprintf ("%%u =%u", $num 1). " <br>//unsigned decimal number (positive) Result:%u = 123456789


Echo sprintf ("%%u =%u", $num 2). " <br>//unsigned decimal number (negative) Result:%u = 4171510507


Echo sprintf ("%%f =%f", $num 1). " <br>//floating-point number (depending on local settings) Result:%f = 123456789.000000
Echo sprintf ("%%f =%F", $num 1). " <br>//floating-point number (does not view local setting) Result:%F = 123456789.000000

Echo sprintf ("%%g =%g", $num 1). " <br> "//shorter than%e and%f results:%g = 1.23457e+8

Echo sprintf ("%%g =%G", $num 1). " <br> "//shorter than%e and%f results:%G = 1.23457E+8

Echo sprintf ("%%o =%o", $num 1). " <br> "//octal number results:%o = 726746425

Echo sprintf ("%%s =%s", $num 1). " <br>//String Result:%s = 123456789

Echo sprintf ("%%x =%x", $num 1). " <br>//16 (lowercase) Result:%x = 75bcd15

Echo sprintf ("%%x =%x", $num 1). " <br>//16 (upper case) Result:%x = 75bcd15

Echo sprintf ("%%+d =%+d", $num 1). " <br>//symbol specifier (positive) Result:%+d = +123456789

Echo sprintf ("%%+d =%+d", $num 2). " <br>//symbol specifier (negative) Result:%+d =-123456789

?>
4. Demo of the string descriptor <?php

$str 1 = "Hello";

$str 2 = "Hello World";


Echo sprintf ("[%s]", $str 1. " <br> ");

Echo sprintf ("[%8s]", $str 1. " <br> ");

Echo sprintf ("[%-8s]", $str 1. " <br> ");

Echo sprintf ("[%08s]", $str 1. " <br> ");

Echo sprintf ("[% ' *8s]", $str 1. " <br> ");

Echo sprintf ("[%8.8s]", $str 2. " <br> ");
?>
Run results: [Hello]                       [Hello]                       [Hello]                       [000Hello]                       [***hello]                       [Hello wo]
















[







] http://:----------------- www.w3school.com.cn/php/func_string_sprintf.asp























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.