PHP sprintf () function usage

Source: Internet
Author: User
Tags cdata numeric lowercase numeric value sprintf

Usage

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

Description
The format parameter is the conversion format. It starts with the percent sign ("%") and ends with the conversion character. The following possible format values:

• %-Return percent sign
• % 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)
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.

The following code is displayed on the WeChat open platform:

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 ();
$ MsgType = "text ";
$ 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> ";
If (! Empty ($ keyword ))
                {
                
$ ContentStr = $ this-> keyrep ($ keyword );
If (empty ($ contentStr ))
     {
$ ContentStr = "You mean it, it's terrible to have no culture"; // You mean it, it's terrible to have no culture;
     }
// $ ContentStr = @ iconv ('utf-8', 'gb2312', $ keyword );
$ ResultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr );
Echo $ resultStr;
     
} Else {
$ ContentStr = 'no culture is terrible, so you can't type it! '; // $ This-> keyrep ($ keyword );
// $ ContentStr = @ iconv ('utf-8', 'gb2312', $ keyword );
$ ResultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr );
Echo $ resultStr;
                }

The above is used in xml. Let's look at another example written by netizens.

The code is as follows: Copy code


<? Php
/**
* Use the sprintf () function
* @ Date 2012-12-17
* @ Author cntnn11
*/
/**
* Manual definition: a function writes formatted strings to a variable.
* His identifiable format
* %-Percentage sign returned
* % B-binary number
* % C-ASCII characters
* % D-signed decimal number
* % E-scientific notation (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 number (uppercase letters)
* Sprintf ($ str, arg1, arg2, arg3 ...);
*/
 
/**
* 1.%
* Replace % with %
*/
$ TestStr = '%' in the test. What will it be replaced ';
Echo sprintf ($ testStr), '<br/> ';
//-> Test the parameter %. What will be replaced;
// Only one % is left
// It seems that only '%' is returned '. But how to use it in practical applications?
// I don't know either ~
Echo '<br/> /**
* 2.% B
* This parameter can only replace integer data. If it is a floating point type, it will only take the integer part. Ignored after decimal point
* If it is a non-integer data, 0 is returned.
*/
$ TestStr = 'I heard that % B will be replaced with the binary number. Is it true? ';
$ Arg = '10 ';
Echo sprintf ($ testStr, $ arg), '<br/> ';
//-> 1010; $ arg = 10; actually replaced!
//-> 101; $ arg = 4.5
//-> 0; $ arg = str/bool...
Echo '<br/>  
/**
* 3.% c returns the ASCII code of the character encoding.
* TIP: [it does not return ASCII code]
* $ Arg receives an int value, that is, the ASCII numeric value, and returns the character corresponding to the value.
*/
$ TestStr = 'Let's test % c: can you return the ASCII code ';
$ Arg = '20140901 ';
Echo sprintf ($ testStr, $ arg );
//-> A; $ arg = 65;
//-> Z; $ arg = 122
Echo '<br/> /**
* 4.% d replace % d in a character segment with int type
* TIP: it can be any int type.
* If it is a floating point data, only the integer part will be replaced.
* If it is not a number, it is replaced with 0.
*/
$ TestStr = "this is an ID, ID: % d ,";
$ Arg = '-4 ';
Echo sprintf ($ testStr, $ arg );
//-> 4; $ arg = 4.5
//-> 0; $ arg = [a-zA-Zs];
Echo '<br/>  
/**
* 5.% e Scientific notation
* TIP: presents a long and long int integer data in scientific notation.
* Same as % d, this function will also ignore the decimal point, and replace any non-numeric data with 0
*/
$ TestStr = "I am very long and have N digits... % E ";
$ Arg = '20140901 ';
Echo sprintf ($ testStr, $ arg );
//-> 4.649846e + 14; $ arg = 464984646548645.64642449463699789789313
//-> 0.20.00e + 0; $ arg = asdfasdf;
Echo '<br/>  
/**
* 5.% u-unsigned decimal number
* Do not understand... If there is a negative number, his value does not know what it is.
*/
$ TestStr = "I am an unsigned decimal number... % U ";
$ Arg = '20140901 ';
Echo sprintf ($ testStr, $ arg );
Echo '<br/>  
/**
* 6.% f-floating point number (local settings aware)
* Is it the opposite of % d?
* This will return a floating point number with a fixed number of six digits after the decimal point.
* The same string is 0;
*/
$ TestStr = "What is the difference with that d? % F ";
$ Arg = '1970. 100 ';
Echo sprintf ($ testStr, $ arg );
Echo '<br/>  
/**
* 7.% F-floating point number (not local settings aware)
* Is it the opposite of % f? How is it no different from small f? No.
*/
$ TestStr = "What is the difference between the lower-case f? % F ";
$ Arg = '1970. 100 ';
Echo sprintf ($ testStr, $ arg );
Echo '<br/>  
/**
* 8.% o-octal numbers
* Same as % d. Only an octal value is passed in the parameter.
*/
$ TestStr = "replace the octal number with % o in decimal format ";
$ Arg = '8 ';
Echo sprintf ($ testStr, $ arg );
Echo '<br/>  
/**
* 9.% x-hexadecimal (lowercase letter)
* Same as % d/% o. Only the parameter is used to input a hexadecimal value of a lower-case letter.
*/
$ TestStr = "replace the hexadecimal number with % o in decimal format ";
$ Arg = '456d12 ';
Echo sprintf ($ testStr, $ arg );
Echo '<br/>  
/**
* 10.% X-hexadecimal (uppercase letters)
* Same as % d/% o/% x. Only the parameter is passed in a hexadecimal value of an upper-case letter.
* It seems that % x % X is case-insensitive...
*/
$ TestStr = "replace the hexadecimal number of uppercase letters with % o in decimal format ";
$ Arg = '456d12 ';
Echo sprintf ($ testStr, $ arg );
Echo '<br/>  
/**
* 11.% s-string
* Replace % s with your input string
*/
$ String = "this is the sprintf string (% s) used for testing ). I spent % f yuan today. There are % d stations from West second flag to Zhichun Road. Work ";
$ Arg = '';
Echo sprintf ($ string, $ arg, 234, 10 );
Echo '<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.