当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > sscanf() 函数
The sscanf() function parses input from a string according to a specified format. The sscanf() function parses a string into variables based on the format string.
sscanf()函数的作用是:按照一定格式解析输入的字符串。
If only two parameters are passed to this function, the data will be returned as an array. Otherwise, if optional parameters are passed, the data parsed are stored in them. If there are more specifiers than variables to contain them, an error occurs. However, if there are less specifiers than variables, the extra variables contain NULL.
如果向该函数传递了两个必要参数,那么数据将以数组的形式返回。否则,如果传递了可选参数,那么结息后的数据将被存入这些参数之中。如果指定的参数值多于变量的总数(这些指定的参数值是赋在变量上的),那么将产生错误;如果指定的参数值少于变量的总数,那么伟被赋值的变量将显示空值。
sscanf(string,format,arg1,arg2,arg++) |
Parameter参数 | Description描述 |
---|---|
string | Required. Specifies the string to read 必要参数。指定需要进行阅读的字符串 |
format | Required. Specifies the format to use. 必要参数。指定要使用的格式 Possible format values:
Additional format values. These are placed between the % and the letter (example %.2f):
Note: If multiple additional format values are used, they must be in the same order as above. |
arg1 | Optional. The first variable to store data in 必要参数。这个自变量(arg1)必须安插在第一个%-符号前 |
arg2 | Optional. The second variable to store data in 可选参数。这个自变量(arg2)必须安插在第二个%-符号前 |
arg++ | Optional. The third, fourth, and so on, to store data in 可选参数。与上述自变量相同,它们可以安插在第三个、第四个……(依次类推)%-符号前。 |
<?php $string = "age:30 weight:60kg"; sscanf($string,"age:%d weight:%dkg",$age,$weight); // show types and values var_dump($age,$weight); ?> |
The output of the code above will be:
上述代码将输出下面的结果:
int(30) int(60) |