Formatting output function in php vprintf printf sprintf sscanf

Source: Internet
Author: User


The vprintf () function outputs formatted strings.

The number format is the conversion format, starting from the percent sign ("%") to the end of the conversion character. The following possible format values:

%-Percentage sign returned
% B-binary number
% C-Characters Based on ascii values
% D-Signed decimal number
% E-resumable counting (for example, 1.5e + 3)
% U-Unsigned decimal number
% F-floating point number (local settings aware)
% F-floating point number (not local settings aware)
% O-octal values
% S-string
% X-hexadecimal (lowercase letter)
% X-hexadecimal (uppercase letters)

View instances
*/
$ Str = "hello"; // define a string
$ Number = 123; // define a value
Vprintf ("% s world. day number % u", array ($ str, $ number); // output formatted strings

// Instance 2
$ Num1 = 123; // defines the value 1
$ Num2 = 456; // defines the value 2
$ Txt = vsprintf ("% f", array ($ num1, $ num2); // output the formatted string to the variable
Echo $ txt;

/*

The printf () function outputs formatted strings.

Syntax
Printf (format, arg1, arg2, arg ++)
Parameters such as arg1, arg2, ++ 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.

*/
$ Str = "hello world"; // defines a string with spaces at the beginning and end
Printf ($ str); // output the converted result

/*

The sprintf () function writes formatted strings to a variable.

Syntax
Sprintf (format, arg1, arg2, arg ++)

*/
$ Str = "hello"; // define a string
$ Number = 123; // define a value
$ Txt = ("% s world. day number % u", $ str, $ number); // returns the formatted string to the variable
Echo $ txt; // The output result hello world. day number 123.


$ Str = "123456 abcdedfbcdef"; // define a string
Sscanf ($ str, "% [^ a-z]", $ result); // retrieves non-UPPERCASE letters
Printf ("result = % s", $ result); // output the result

/* The following code displays 11 and writes the "hello world" character string to the test.txt file */
$ Str = "hello"; // define a string
$ File = fopen ("test.txt", "w"); // open the file
Echo vfprintf ($ file, "% s world", $ str); // output data to the stream and display result 11

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.