sprintf function PHP's detailed use method, sprintf function php
PHP sprintf () function
First of all, why write the preface to this function, this is I developed in two times a token verification document is a sample document see a function, then very do not understand, then looked at Baidu, but a lot of results are very general, the results are very few, Later consulted the 3c and asked the company some people did some tests slowly understand some, is my personal opinion of the sprintf function
Useful: Format strings in multiple types
For: When working with XML data formats, you need to use him to format and so on.
basic Syntax Format: sprintf ("% format type", "$str 1", "$str 2"); don't worry, I'll say it slowly.
Look at the Type reference table first, that is, what type of format to convert to
This is the Type reference table for the conversion format
Let's start with a simple case.
PHP$str 1= "1234"; Echo sprintf ("hello%s", "$str 1"); // The effect is: hello1234?>
What does that mean?
Points:
The %s = % symbol and the trailing attribute symbol (s) are always called Insert tag combinations , which are the values ($str 1) that are prepared to be formatted later in this position
Hello = This word is a lot of people blinded by the place, tell you this is not a representative, simply represent a hello, used for segmentation or modification, generally used [%s], <%s> so formatted directly in the label
Remember that there is only one type attribute behind a % marker (for example, s) , S is what is above, formatted in a string way
So how many values are formatted together?
See
PHP$a= "abcdef"; $b= "abcdef"; $c= "1234"; Echo sprintf ("%1\ $s%2\ $s",$c,$a); // The effect is: 1234abcdef?>
%s is a tag, two %s%s This is wrong, each%s must flag the keys, or how I know all represents the format behind which $str, so there is a special syntax
%1\$%2\$ Explanation:%1 represents the corresponding $STR1 in formatted sprintf ("%1\$%2\$", "$str 1", "$str 2") , then%2 naturally represents formatting $str2,\$ is to indicate that there are multiple values ready to be formatted, so each%1 and%2 or the tag with%3 will be marked with this symbol for multiple tokens in a row, and if only one tag is not \$ the placeholder, remember that $str2, $STR 3 is optional, that is, you can not format so much
Tell me a special example.
PHP$a= "abcdef"; $b= "abcdef"; $c= "1234"; Echo sprintf ("% ' x13.2f",$c); // The effect is: Xxxxxx1234.00//echo sprintf ("%06.2f", $a);? >
sprintf ("% ' x13.2f", $c);
What does this mean, F is a floating-point number, the first step in the format % ' (complement value) width value format type of these three parts, the syntax must be next to each other with no space
You must explain what the complement value is: the width of the setting is exceeded, use this value to fill in
Explain that the complement value is only set to the width beyond the target value.
So just use X to fill in, 13 is the total width, 2 is the width after the decimal point, F is the format type, don't worry I will explain
' Number (single quote) represents the next fill type
Why is he able to recognize that x is a complement, because there's a ' number in front,
Why he can identify which of the types, he is so identified, in order binate from the two characters start to identify what type, the complement value is definitely a single digit, it is not possible to fill a position of two number bar, so the left first x is the complement value, the right first bit is the format type, and then the middle of nature is the width value
Second, why there is a property after the decimal point, because this is f (floating point), must be set after the decimal point there are several, cannot be set after the decimal point, what is the meaning of floating points?
Don't get bored, every example is condensed.
Integer-Complement
PHP$a= "abcdef"; $b= "abcdef"; $c= "1234"; Echo sprintf ("% ' 07s",$c); // The result is: 0001234?>
This is the integer complement, or the same
The first step is to format the type% ' (complement value) width value by The three parts of the
0 is the complement value 7 is the Width value s nature is the formatted type
And one of the most important examples
PHP$a= "abcdef"; $b= "abcdef"; $c= "1234"; Echo sprintf ("[%-6s]",$c); // The result is: [1234] Echo sprintf ("[%-4s]",$c); // The result is: [1234] Echo sprintf ("[%4.2s]",$c); // The result is: [?>]
This first step [] is merely a modification, without understanding
The second step, no ' number, proves no fill, no need to add a complement value
So the syntax format is: % width value format type these two parts
The 12th line is explained as follows:
The first width is 6, but $c=1234, only 4 characters, so the width is not enough, so the right auto-expansion (expansion and more also only show a space position), why expand on the right, because there is a number in front of the width, the opposite of the complement direction, such as before the complement value-, the natural starting from the right to fill
Why the following is not changed, because the width is exactly the same, but the direction of the complement changed
The third line is explained as follows:
Don't be fooled, the grammatical structure is the same. % width value format type these two parts
So 4.2 is still the width value
Just the decimal point to the left of the 4 represents the total width, the right 2 represents only 2 bits out, so there are two vacancies, so the left to expand the two empty, why only display a space on the paragraph said, say again the expansion is more than just a space position. Default starts from left
Believe has been thoroughly said, has not been able to concentrate the place, what do not understand can leave a message, usually in, as far as possible to help you solve, put out a small hand point of praise, thank you
http://www.bkjia.com/PHPjc/1140785.html www.bkjia.com true http://www.bkjia.com/PHPjc/1140785.html techarticle sprintf function php Detailed use method, sprintf function php php sprintf () function First say why write this function preface, this is I developed in two times a token verification text ...