PHP sscanf()Functions

PHP String 参考手册PHP String Reference Manual

Examples

Parse a string:

<?php
$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:

  • printf()- Output a formatted string
  • sprintf()- Write a formatted string to a variable

Syntax

sscanf(string,format,arg1,arg2,arg++)

Parameters Description
string Required. Specifies the string to be read.
format Required. Specifies the format to use.

Possible format values:

  • %% - Returns a percent sign %
  • %c - Character corresponding to the ASCII value
  • %d - Decimal number including sign (negative, zero, positive)
  • %e - Scientific notation using 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. Must be placed between % and the letter (e.g. %.2f):

  • + (Adds + or - before a number to define the sign. By default, only negative numbers are marked, positive numbers are not)
  • ' (Specifies what to use as padding, default is space. It must be used with the width specifier. For example: %'x20s (using "x" as padding))
  • - (Left-adjust the variable value)
  • [0-9] (Specifies the minimum width of the variable value)
  • .[0-9] (Specifies the number of decimal places or maximum string length)

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:

<?php
$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 参考手册PHP String Reference Manual Other Extensions