PHP writes a formatted string to a function in a variable vsprintf ()

Source: Internet
Author: User
Tags sprintf

Instance

Write the formatted string to the variable:

<?php$number = 9; $str = "Beijing"; $txt = vsprintf ("There is%u million bicycles in%s.", Array ($number, $str)); Echo $txt ;? >

Definition and usage

The vsprintf () function writes a formatted string to a variable.

Unlike sprintf (), the parameters in vsprintf () are in the array. The array elements are inserted into the percent sign (%) symbol in the main string. This function is executed step-by. At the first% symbol, insert the first array element, insert the second array element at the second% symbol, and so on.

Note: If the% symbol is more than the arg parameter, you must use a placeholder. Placeholders are inserted after the% symbol, consisting of numbers and "\$". See Example 2.

Tip: Related functions: fprintf (), vfprintf (), printf (), sprintf (), and vprintf ()

Grammar

vsprintf (Format,argarray)
Parameters Describe
Format Necessary. Specifies the string and how to format the variable.

Possible format values:

    • Percent percent-returns a percent semicolon%

    • %b-Binary number

    • The character that corresponds to the%C-ASCII value

    • %d-decimal number with sign (negative, 0, positive)

    • %e-Using the lowercase scientific notation (e.g. 1.2e+2)

    • %E-Use uppercase scientific notation (e.g. 1.2E+2)

    • %u-decimal number with no sign (greater than or equal to 0)

    • %f-Floating point number (local setting)

    • %F-Floating point number (non-local setting)

    • %g-Shorter%e and%f

    • %G-Shorter%E and%f

    • %o-Eight binary number

    • %s-String

    • %x-16 binary number (lowercase letters)

    • %x-16 decimal digits (uppercase letters)

The attached format value. Must be placed between% and letter (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.

    • ' (specifies what to use as a fill, the default is a space.) It must be used with the width of the specified device. For example:% ' x20s (using "X" as the fill))

    • -(left adjustment variable value)

    • [0-9] (minimum width of the specified variable value)

    • . [0-9] (Specify the number of decimal digits or the maximum string length)

Note: If you use more than one of the above format values, they must be used in the order above and cannot be disrupted.

Argarray Necessary. An array with parameters that are inserted into the% symbol in the format string.

Technical details

return value: Returns an array value in the form of a formatted string.
PHP version: 4.1.0+

More examples

Example 1

Use the format value%f:

<?PHP$NUM1 = 123; $num 2 = 456; $txt = vsprintf ("%f%f", Array ($num 1, $num 2)); Echo $txt;? >

Example 2

Use placeholders:

<?php$number = 123; $txt = vsprintf ("With 2 decimals:%1$.2f<br>with no decimals:%1$u", Array ($number)); Echo $txt ;? >

Example 3

Use sprintf () to demonstrate all possible format values:

&LT;?PHP$NUM1 = 123456789; $num 2 = -123456789; $char = 50; The ASCII Character is 2//note:the format value "percent" returns a percent Signecho sprintf ("%%b =%b", $num 1). " <br> "; Binary Numberecho sprintf ("%%c =%c", $char). " <br> "; The ASCII characterecho sprintf ("%%d =%d", $num 1). " <br> "; Signed decimal Numberecho sprintf ("%%d =%d", $num 2). " <br> "; Signed decimal Numberecho sprintf ("%%e =%e", $num 1). " <br> "; Scientific notation (lowercase) echo sprintf ("%%e =%E", $num 1). " <br> "; Scientific notation (uppercase) echo sprintf ("%%u =%u", $num 1). " <br> "; Unsigned decimal number (positive) echo sprintf ("%%u =%u", $num 2). " <br> "; Unsigned decimal number (negative) echo sprintf ("%%f =%f", $num 1). " <br> "; Floating-point number (Local settings Aware) echo sprintf ("%%f =%F", $num 1). " <br> "; Floating-point number (not local sett aware) echo sprintf ("%%g =%g", $num 1). " <br> "; Shorter of%e and%fecho sprintf ("%%g =%G ", $num 1)." <br> "; Shorter of%E and%fecho sprintf ("%%o =%o", $num 1). " <br> "; Octal Numberecho sprintf ("%%s =%s", $num 1). " <br> "; Stringecho sprintf ("%%x =%x", $num 1). " <br> "; Hexadecimal number (lowercase) echo sprintf ("%%x =%x", $num 1). " <br> "; Hexadecimal number (uppercase) echo sprintf ("%%+d =%+d", $num 1). " <br> "; Sign specifier (positive) echo sprintf ("%%+d =%+d", $num 2). " <br> "; Sign specifier (negative)?>

Example 4

A demo of the string specifier:

<?PHP$STR1 = "Hello"; $str 2 = "Hello world!"; Echo vsprintf ("[%s]", Array ($str 1)). " <br> "; Echo vsprintf (" [%8s] ", Array ($str 1))." <br> "; Echo vsprintf (" [%-8s] ", Array ($str 1))." <br> "; Echo vsprintf (" [%08s] ", Array ($str 1))." <br> "; Echo vsprintf ("[% ' *8s]", Array ($str 1)). " <br> "; Echo vsprintf (" [%8.8s] ", Array ($str 2))." <br> ";?>

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.