Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP sscanf() Function

PHP sscanf() Function

PHP String Reference Manual PHP String Reference Manual

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:

  • %% - Returns a percent sign %
  • %c - The character corresponding to an ASCII value
  • %d - Decimal number with sign (negative, 0, positive)
  • %e - Scientific notation in lowercase (e.g. 1.2e+2)
  • %u - Decimal number without sign (greater than or equal to 0)
  • %f - Floating-point number
  • %o - Octal number
  • %s - String
  • %x - Hexadecimal number (lowercase letters)
  • %X - Hexadecimal number (uppercase letters)
  • Additional format values. They must be placed between the % and the letter (for example %.2f):

    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 PHP String Reference Manual