Home >>PHP String Functions >PHP sscanf() Function
PHP sscanf() function is used to scan parse input from a given input string according to a specified format. This function parses a string into variables based on the given format string. If we pass two parameters, this function will return the data as an array.
Syntax:
sscanf($string,$format,$arg1,$arg2,$arg++);
Parameter | Description |
---|---|
string | This is a required parameter. This parameter contains the string to be read. |
format | This is a required parameter. This parameter defines the formatto be used. Some of the possible format values:
|
arg1 | This is a required parameter. This parameter contains the argument to be inserted at the first %-sign. |
arg2 | This is an optional parameter. This parameter contains the argument to be inserted at the second %-sign. |
arg++ | This is an optional parameter. This parameter contains the argument to be inserted at the upcoming %-sign. |
Here is an example of sscanf() function in PHP:
<html> <body> <?php $str = "age:21 weight:54kg"; sscanf($str,"age:%d weight:%dkg",$age,$weight); // show types and values var_dump($age,$weight); ?> </body> </html>
Here is an another example of sscanf() function in PHP:
<html> <body> <?php $str = "Someone is 21 years old and earns 20000 per month ."; $format = sscanf($str,"%s %s %d %s %s %s %s %d %s %s %c"); print_r($format); ?> </body> </html>