PHP sscanf() Function


The sscanf() function is used to return parses input from a string according to a specified format. If pass two parameters only in the function, the data will be returned as an array

Syntax
sscanf(string,format,arg1,arg2,arg++)
Parameters
Parameter Description
String (Required) Specify the read to string.
Format (Required) Specify the format to used.
%% It returns a percent (%) sign.
%c ASCII value
%d Signed decimal number.
%e Scientific notation with lowercase, e.g., 1.2e+2.
%u Unsigned decimal number(= or >0)
%f Floating-point number.
%o Represented as an octal number.
%s String.
%x It is represented as a hexadecimal number with lowercase letters.
%X It is represented as a hexadecimal number with Uppercase letters.
arg1 (optional) Specify first variable to added data
arg2 (optional) Specify second variable to added data
arg++ (optional) Specify third , fourth and so on.
Example
<?php
   $a = "height:5 weight:70kg";
   sscanf($a,"height:%d weight:%dkg",$height,$weight);
   var_dump($height,$weight);
?>    

Output

int(5) int(70)
Example
<?php  
    $a= "We are going tour 20 july";  
    $b = sscanf($a,"%s %s %s %s %c %c %s");  
    print_r($b);  
?>    

Output

Array ( [0] => We [1] => are [2] => going [3] => tour [4] => 2 [5] => 0 [6] => july )

Prev Next