We know that the sprintf () function writes a formatted string to a variable, and we often see this type of code, let me introduce the sprintf () function using the method, a friend who needs to refer to the reference.
Usage
sprintf (format,arg1,arg2,arg++)
Description
The parameters format is the converted format, starting with the percent sign ("%") to the end of the converted character. The following possible format values are:
%%-return percent symbol
%b-binary number
%c-Characters in accordance with ASCII values
%d-Signed decimal number
%e-Sustainable counting method (e.g. 1.5e+3)
%u-Unsigned decimal number
%f-Floating point (Local settings Aware)
%f-Floating point number (not local settings aware)
%o-Eight binary number
%s-String
%x-16 decimal digits (lowercase letters)
%x-16 decimal digits (uppercase letters)
Arg1, arg2, + + etc parameters will be inserted into the main string percent percent (%) Symbol. This function is executed step-by. In the first% symbol, insert arg1, insert arg2 at the second% symbol, and so on.
I will see the following code on the 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 = " %s %s %s %s %s 0 "; if (!empty ($keyword)) { $CONTENTSTR = $this->keyrep ($keyword); if (empty ($CONTENTSTR)) { $CONTENTSTR = "You deliberately, no culture is terrible";//You deliberately, no culture is really scary; } $CONTENTSTR = @iconv (' UTF-8 ', ' gb2312 ', $keyword); $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR); Echo $resultStr; }else{ $contentStr = ' No culture really scary, can't actually type! ';//$this->keyrep ($keyword); $CONTENTSTR = @iconv (' UTF-8 ', ' gb2312 ', $keyword); $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR); Echo $resultStr; } |
The above is used in the XML, the following look at a netizen to write an example
The code is as follows |
Copy Code |
/** * the sprintf () function uses * @date 2012-12-17 * @author Cntnn11 */ /** * Manual definition: The function writes a formatted string to a variable. * His recognizable format * Percent%-return percentage symbol *%b-binary number *%c-Characters in accordance with ASCII values *%d-Signed decimal number *%e-scientific notation (e.g. 1.5e+3) *%u-Unsigned decimal number *%f-floating point (Local settings Aware) *%F-floating point number (not local settings aware) *%o-Eight binary number *%s-string *%x-16 binary number (lowercase letter) *%x-16 binary number (capital letter) * sprintf ($STR, Arg1, arg2, arg3 ...); */
/** * 1. %% * Replace percent to% */ $TESTSTR = ' Test the percent of this parameter. will be replaced by what? '; echo sprintf ($testStr), ' '; Test% of this parameter. will be replaced by what? There's only one percent left. It seems really just to return a '% '. But how do you use it in real-world applications? And I don't know ~ Echo '
'; /** * 2. %b * This parameter can only replace integer data. If it is a floating-point type, then he will only take the integer part. After the decimal point will be ignored * If it is a non-integer data, then return 0 */ $TESTSTR = ' heard that%b will be replaced with binary numbers, really? '; $arg = ' 10 '; Echo sprintf ($TESTSTR, $arg), ' '; -1010; $arg = 10; It's really a replacement! -101; $arg =4.5 -0; $arg =str/bool ... Echo ' '; /** * 3. %c returns the ASCII code for character encoding * tip:[He is not returning ASCII code] * $arg accepts an int. numeric value that is passed in as ASCII and returns the character corresponding to that value */ $TESTSTR = ' Let's Test the next%c: try to return the ASCII code '; $arg = ' 122 '; Echo sprintf ($TESTSTR, $arg); A; $arg = 65; -Z; $arg =122 Echo ' '; /** * 4. %d replaces a segment of character%d with type int * TIP: This can be any int integer type. * If it is a floating-point data, it will only replace the integral part of the * If it is non-digital, then replace it with 0 */ $TESTSTR = "This is a id,id number is%d,"; $arg = '-4 '; Echo sprintf ($TESTSTR, $arg); -4; $arg =4.5 -0; $arg =[a-za-zs]; Echo ' '; /** * 5. %e Scientific Counting method * TIP: A long, long, int integer data is presented in the form of scientific notation * Same as%d, the function will also ignore the decimal point, any non-numeric data replaced by 0 */ $TESTSTR = "I am very long, there are n many ... %e "; $arg = ' 46498464654864564642449463699789789313 '; Echo sprintf ($TESTSTR, $arg); -4.649846e+14; $arg =464984646548645.64642449463699789789313 -0.000000e+0; $arg =ASDFASDF; Echo ' '; /** * 5. %u-Unsigned decimal number * Don't understand ... If there is a negative number, he doesn't know what the value is. */ $TESTSTR = "I am an unsigned decimal number ... %u "; $arg = ' 456 '; Echo sprintf ($TESTSTR, $arg); Echo ' '; /** * 6. %f-Floating point (Local settings Aware) * Is it the opposite of%d? * This will return a floating-point number, and only a fixed 6-bit after the decimal point * The string is also 0; */ $TESTSTR = "What's the difference between that and d?" %f "; $arg = ' 456.235645 '; Echo sprintf ($TESTSTR, $arg); Echo ' '; /** * 7. %F-Floating point number (not local settings aware) * Is it the opposite of%f? What's the difference between a little f? No way */ $TESTSTR = "What's the difference between that and that lowercase f?" %F "; $arg = ' 12312316.46898 '; Echo sprintf ($TESTSTR, $arg); Echo ' '; /** * 8. %o-Eight binary number * Same as%d. Only the parameter is passed in an octal value */ $TESTSTR = "Replace octal number with decimal%o"; $arg = ' 8 '; Echo sprintf ($TESTSTR, $arg); Echo ' '; /** * 9. %x-16 binary number (lowercase letters) * Same as%d/%o. Only the parameter is passed in a lowercase hexadecimal value */ $TESTSTR = "Replace hexadecimal number with decimal%o"; $arg = ' 456d12 '; Echo sprintf ($TESTSTR, $arg); Echo ' '; /** * 10. %x-16 decimal digits (uppercase letters) * Same as%d/%o/%x. Only the parameter is passed in a hexadecimal value of an uppercase letter * Seemingly%x%x two letter case No difference ... */ $TESTSTR = "Replace the hexadecimal number of uppercase letters with the decimal%o"; $arg = ' 456d12 '; Echo sprintf ($TESTSTR, $arg); Echo ' '; /** * 11. %s-String * Replace%s with the string you passed in */ $string = "This is the string used to test the sprintf (%s). Today, we consumed%f yuan. There are%d stops from the West two flags to the known spring Road. Work "; $arg = "; Echo sprintf ($string, $arg, 234, 10); Echo ' '; ?> |
http://www.bkjia.com/PHPjc/628892.html www.bkjia.com true http://www.bkjia.com/PHPjc/628892.html techarticle we know that the sprintf () function writes a formatted string to a variable, and we often see this type of code, and let me introduce the sprintf () function using the method, a friend in Need ...