PHPsprintf () function usage. We know that the sprintf () function writes formatted strings into a variable, and we often see this type of code. next I will introduce how to use the sprintf () function, if you have any need, we know that the sprintf () function writes formatted strings into a variable. we often see this type of code. next I will introduce how to use the sprintf () function, for more information, see.
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 open platform:
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 (); $ MsgType = "text "; $ TextTpl =" %s %s % S %s %s 0 "; 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: |
|
/** * 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 ),' '; //-> 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'
'; /** * 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 ),' '; //-> 1010; $ arg = 10; actually replaced! //-> 101; $ arg = 4.5 //-> 0; $ arg = str/bool... Echo' '; /** * 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' '; /** * 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' '; /** * 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' '; /** * 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' '; /** * 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' '; /** * 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' '; /** * 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' '; /** * 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' '; /** * 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' '; /** * 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' '; ?> |
The trim () function writes formatted strings into a variable. we often see this type of code. next I will introduce the use of the sprintf () function, a friend who needs it...