w3resource

PHP: sscanf() function

PHP: Parses input from a string according to a format

The sscanf() function parses string into variables based on the specified format.

Version:

(PHP 4 and above)

Syntax:

sscanf(input_string, format_string, arg1, arg2, arg3....)

Parameters:

Name Description Required /
Optional
Type
input_string The argument to process in radians Required String
format Each conversion specification starts with a single percent sign (%) and ends with the following conversion characters.
% - returns a percent sign.
b - the argument is treated as an integer and display it as a binary number.
c - the argument is treated as an integer and display it as an ASCII value.
d - the argument is treated as an integer and display as a signed decimal number.
e - the argument is treated as scientific notation (e.g. 1.2e+2).
E - the argument treated as scientific notation (e.g.1.2E+2).
u - the argument is treated as an integer, and display as an unsigned decimal number.
f- the argument is treated as a float, and display as a floating-point number. (local aware)
F - the argument is treated as a float, and display as a floating-point number (non-locale aware).
g - shorter of %e and %f.
G - shorter of %E and %f.
o- the argument is treated as an integer, and display as an octal number.
s - the argument is treated as string and display as a string.
x - the argument is treated as an integer and display as a hexadecimal number (with lowercase letters).
X - the argument is treated as an integer and display as a hexadecimal number (with uppercase letters).

Optional specification :
Sign specifier : display a sign (+ or -) in front a number. By default a - sign is used in front of a number if it is negative.

Padding character :
Default character is space. An alternate padding character can be specified by prefixing it with a single quote.
Alignment specifier. : - character makes the alignment left-justified. The default is right justified.
Width specifier : An integer number specifies the width of the field.
Precision specifier : The argument specifies how many decimal number should be displayed for floating numbers. When using this specifier on a string, it acts as a cutoff point, setting a maximum character limit to the string.
Required String
arg1 The argument to be added as the first %-sign in the formatted string. Required Mixed
arg2, arg3. These arguments will be added as second %, third % etc. in the formatted string. Optional Mixed

*Mixed : Mixed indicates that a parameter may accept multiple (but not necessarily all) types.

Return value

If only two parameters were passed to this function, the values parsed will be returned as an array.
If optional parameters are passed, the function will return the number of assigned values. The optional parameters must be passed by reference.

Value Type: Mixed

Pictorial Presentation

php-string-sscanf()

Example:

<?php
$input_string = 'Marks : Math - 88 :: Language - 75';
sscanf($input_string, 'Marks : Math - %d :: Language - %d', $math, $language);
var_dump($math, $language);
?> 

Output:

int(88)  int(75)

View the example in the browser

See also

PHP Function Reference

Previous: sprintf
Next: str_repeat



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/php/function-reference/sscanf.php