Home  >  Article  >  Backend Development  >  How to use php sscanf function

How to use php sscanf function

青灯夜游
青灯夜游Original
2019-05-27 09:27:492158browse

PHP sscanf() function is used to parse input from a string according to the specified format. The syntax is sscanf(string,format,arg1,arg2,arg). The sscanf() function parses a string into a variable based on the format string.

How to use php sscanf function

How to use php sscanf() function?

The sscanf() function parses input from a string according to the specified format. The sscanf() function parses a string into a variable based on a format string.

Syntax

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

Parameters:

1, string: required. Specifies the string to be read.

2. format: required. Specifies the format to be used. Possible format values:

%% - returns a percent sign %

%c - the character corresponding to the ASCII value

%d - contains the plus and minus signs Decimal number (negative, 0, positive)

 ● %e - Use lowercase scientific notation (e.g. 1.2e 2)

 ● %u - Decimal number without sign Number (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 value. Must be placed between % and letters (such as %.2f):

 ◆  (Add or - in front of the number to define the sign of the number. By default, only negative numbers are marked, and positive numbers are not. Mark)

 ◆ (Specifies what to use as padding, defaults to spaces. It must be used with a width specifier.)

 ◆ - (Left adjustment variable value)

 ◆ [0-9] (Specify the minimum width of variable value)

 ◆ .[0-9] (Specify the number of decimal places or the maximum string length)

Note: If you use multiple The above format values ​​must be used in the order above.

3. arg1: optional. The first variable to store data.

4. arg2: optional. A second variable to store the data.

5. arg: optional. The third and fourth variables store data, and so on.

Return value: If only two parameters are passed to this function, the data will be returned in the form of an array. Otherwise, if additional parameters are passed, the parsed data will be stored in these parameters. If the number of separators is greater than the number of variables containing them, an error occurs. However, if the number of separators is less than the number of variables that contain them, the extra variables contain NULL.

Let’s take a look at how to use the php sscanf() function through an example.

Example 1:

<?php
$str = "age:30 weight:60kg";
sscanf($str,"age:%d weight:%dkg",$age,$weight);
// 显示类型和值
var_dump($age,$weight);
?>

Output:

int(30) int(60)

Example 2:

<?php
$str = "If you divide 4 by 2 you&#39;ll get 2";
$format = sscanf($str,"%s %s %s %d %s %d %s %s %c");
print_r($format);
?>

Output:

Array ( [0] => If [1] => you [2] => divide [3] => 4 [4] => by [5] => 2 [6] => you&#39;ll [7] => get [8] => 2 )

The above is the detailed content of How to use php sscanf function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn