PHP sscanf()Functions
Examples
Parse a string:
$str = "age:30 weight:60kg";
sscanf($str,"age:%d weight:%dkg",$age,$weight);
// show types and values
var_dump($age,$weight);
?>
Run Example »
The sscanf() function parses input from a string according to the specified format. The sscanf() function parses a string into variables based on the format string.
If only two arguments are passed to the function, the data will be returned as an array. Otherwise, if additional parameters are passed, the parsed data will be stored in these parameters. If the number of specifiers is greater than the number of variables containing them, an error occurs. However, if the number of specifiers is less than the number of variables containing them, the extra variables contain NULL.
Related functions:
Syntax
| Parameters | Description |
|---|---|
| string | Required. Specifies the string to be read. |
| format | Required. Specifies the format to use. Possible format values:
Additional format values. Must be placed between % and the letter (e.g. %.2f):
Note:If multiple of the above format values are used, they must be used in the order above and cannot be rearranged. |
| arg1 | Optional. The first variable to store data. |
| arg2 | Optional. The second variable to store data. |
| arg++ | Optional. The third, fourth variables to store data. And so on. |
Technical Details
| Return value: | If only two arguments are passed to the function, the data will be returned as an array. Otherwise, if additional parameters are passed, the parsed data will be stored in these parameters. If the number of specifiers is greater than the number of variables containing them, an error occurs. However, if the number of specifiers is less than the number of variables containing them, the extra variables contain NULL. |
|---|---|
| PHP Version: | 4.0.1+ |
More Examples
Example 1
Use format values %s, %d, and %c:
$str = "If you divide 4 by 2 you'll get 2";
$format = sscanf($str,"%s %s %s %d %s %d %s %s %c");
print_r($format);
?>
Run Example »
PHP String Reference Manual Other Extensions