PHP sscanf() Function
Example
Parse a string:
Run Example »
The sscanf() function parses input from a string according to a specified format. The sscanf() function parses a string into variables based on a format string.
If only two parameters are passed to this function, the data is returned as an array. Otherwise, if extra parameters are passed, the parsed data is stored in those parameters. If the number of format specifiers exceeds the number of variables, an error occurs. However, if the number of specifiers is less than the number of variables, the extra variables contain NULL.
Related functions:
Syntax
sscanf(
string,format,arg1,arg2,arg++
)
| Parameters | Description |
|---|---|
| string | Required. Specifies the string to read. |
| format | Required. Specifies the format to use.
Possible format values: |
Additional format values. They must be placed between the % and the letter (for example %.2f):
- + (Prefixes the number with + or - to define its sign. By default, only negative numbers are marked; positive numbers are not)
- ' (Specifies what to use as padding; default is a space. It must be used together with a width specifier. For example: %'x20s (uses "x" as padding))
- - (Left-aligns the variable value)
- [0-9] (Specifies the minimum width of the variable value)
- .[0-9] (Specifies the number of decimal places or the maximum string length)
Note:If multiple of the above format values are used, they must be used in the order shown above and cannot be rearranged.
| | *arg1* | Optional. The first variable to store the data. | | *arg2* | Optional. The second variable to store the data. | | *arg++* | Optional. The third, fourth, etc. variables to store the data. |Technical Details
| Return Value: | If only two parameters are passed to this function, the data is returned as an array. Otherwise, if extra parameters are passed, the parsed data is stored in those parameters. If the number of format specifiers exceeds the number of variables, an error occurs. However, if the number of specifiers is less than the number of variables, the extra variables contain NULL. |
|---|---|
| PHP Version: | 4.0.1+ |
More examples
Example 1
Use the format values %s, %d and %c:
Run Example »
PHP String Reference Manual