When the payment SDK is received, the third party callback processing requires IP, and the IP needs are: Remove the dot, 0 to 3 bits per address segment, such as: 192168000001
Let's take a look at my implementation:
1<?PHP2 $IP=Explode('. ', ' 192.168.1.12 ' );3 $NewIP="";4 for($i= 0;$i<Count($IP);$i++) {5 $NewIP.=Str_pad($IP[$i], 3, "0",str_pad_left);6 }7 Echo $NewIP;8?>
Results: 192168001012
PHP has two functions, can achieve the number of 0 format, Str_pad (), sprintf ()
Now analyze these two functions in detail:
1:str_pad
As the name implies, Str_pad is for a string that can fill any other string with the specified string.
For example: str_pad (need to fill the string, fill the length, fill the string, fill the position)
The length of the fill must be a positive integer, and the fill position has three options:
Left: Str_pad_left,
Right: Str_pad_right,
Ends: Str_pad_both
For example:
Echo Str_pad (1, 8, "0″,str_pad_left");
Results: 00000001
Echo Str_pad (1, 8, "0″,str_pad_right");
Results: 10000000
Echo Str_pad (1, 8, "0″,str_pad_both");
Results: 00010000
One of the notable details in the above example is that if the number of digits filled is odd, such as example three fills 7 0, the right priority.
2:sprintf
A: Left complement 0
Echo sprintf ("%05d", 1);
First say the meaning of%05d, with a 5-digit number to format the parameters behind, if less than 5 0
Operation result is 00005
B: 0 after the decimal point
Echo sprintf ("%01.3f", 1);
%01.3f means that a minimum of three digits after a decimal point is less than three 0, at least one digit before the decimal point, and a floating-point number that is less than a zero complement is formatted with the parameters behind it.
The operation result is: 1.000
About the two methods of zero complement you can choose to use, in fact, each has the pros and cons, sprintf can ensure that you do not mistakenly operate it 1 1000000 haha, Str_pad can guarantee you want to fill what.
Implementation of the PHP implementation of digital complement 0 format