PHP sscanf function

 The PHP sscanf function can parse a string variable according to the specified format and format the parsed part, that is to say, you can use the sscanf function to parse and read some parts of string , and then Process according to the virtual format, and finally obtain the required string data. The effect is opposite to the functions such as printf and sprintf . Although all the actions of formatting strings are processed through regular expressions functions such as printf and sprintf Insert characters into the string , and the sscanf function parses the characters from the string .


Basic syntax of PHP sscanf function

mixed sscanf ($string, $format, $args1, $args2, $args3, $args4 ...)


sscanf function within parentheses first $ string argument to the original string , which is about to read is parsed string , the second parameter $ format is the format you want to use, sscanf function based on $ format format , Parse the $string string , and store the parsed result through the third starting $args. For the conversion format of $format, please refer to the parameter table on the sprintf function . Note that the sscanf function does not support F, g, G, b and other formats. The two parameters $string and $format are required items. The storage parameters of $args at the back are optional items and the number is determined by yourself. However, if the percentage (%) in the $format parameter exceeds the number of $args, an error message will appear.

PHP sscanf function example
<?php
$string = "This is float 3.2 and this is integer 8";
sscanf($string,"This is float %f and this is integer %d",$float,$integer);
var_dump($float,$ integer);
?>
The output of the above example is
float(3.2) int(8)
The string $string in the example has two numbers, the floating-point number 3.2 and the integer 8. We use the formatting function of the sscanf function to capture these two numbers. To parse the floating-point number 3.2, you must In $format, write the format as %f. To parse out the number 8, you must write the format as %d in $format, and then display the data type and data content of the analysis result on the web page once through var_dump . float(3.2) represents the floating point number 3.2, int(8) represents the integer 8.

Post a Comment

0 Comments