Testing sscanf() support in PHP


Testing sscanf with standard ANSI syntax (values returned by reference)-


Simple Test

sscanf('10','%d',&$decimal)

BEFORE :
decimal = -1.
AFTER :
decimal = 10


Simple Test 2

sscanf("ghost 0xface","%s %x",&$string, &$int)

BEFORE :
string = , int = -1

AFTER :
string = ghost, int = 64206
sscan reports : 2 conversions


Multiple specifiers

sscanf("jabberwocky 1024 0xFF 1.024 644 10", "%s %d %x %f %o %i", &$string,&$decimal,&$hex,&$float,&$octal,&$int);

BEFORE :
Decimal = 10, String = ghost, Hex = 0
Octal = 0 , Float = 0, Int = 64206

AFTER :
decimal = 1024, string = jabberwocky, hex = 255
octal = 420 , float = 1.024, int = 10
sscan reports : 6 conversions
----------------------------------------


Running Test Cases from scan_cases

** Case : valid decimal (positive) ******************************

sscanf("48,"%d").
-----------------
Result : 48
-----------------

** Case : valid signed decimal (negative) ******************************

sscanf("-98,"%d").
------------------
Result : -98
------------------

** Case : integer scan : decimal digit followed by alpha ******************************

sscanf("56a,"%d").
------------------
Result : 56
------------------

** Case : decimal integer with width specification ******************************

sscanf("558071,"%4d").
----------------------
Result : 5580
----------------------

** Case : valid signed integer (negative) ******************************

sscanf("-5489,"%i").
--------------------
Result : -5489
--------------------

** Case : plain ole string matched with %s ******************************

sscanf(" the rain in spain ,"%s").
----------------------------------
Result : the
----------------------------------

** Case : string with width specifier ******************************

sscanf("jabberwocky,"%10s").
----------------------------
Result : jabberwock
----------------------------

** Case : valid float (%f) ******************************

sscanf("289.071,"%f").
----------------------
Result : 289.071
----------------------

** Case : valid negative (%f) ******************************

sscanf("-0.403,"%f").
---------------------
Result : -0.403
---------------------

** Case : Float with width specifier ending at decimal point ******************************

sscanf("76.4,"%3f").
--------------------
Result : 76
--------------------

** Case : Float with width specifier ******************************

sscanf("789.4,"%3f"").
----------------------
Result : 789
----------------------

** Case : octal with leading 0 ******************************

sscanf("0321,"%o").
-------------------
Result : 209
-------------------

** Case : valid octal digits ******************************

sscanf("327,"%o").
------------------
Result : 215
------------------

** Case : octal scan with octal digit followed by non-octal ******************************

sscanf("380,"%o").
------------------
Result : 3
------------------

** Case : valid hex ******************************

sscanf("fe,"%x").
-----------------
Result : 254
-----------------

** Case : "c" style hex with leading 0x ******************************

sscanf("0xfe,"%x").
-------------------
Result : 254
-------------------

** Case : hex with all single digits < f ******************************

sscanf("455,"%x").
------------------
Result : 1109
------------------

** Case : hex (negative signed int) ******************************

sscanf("-98,"%x").
------------------
Result : -152
------------------

** Case : single char ******************************

sscanf("y,"%c").
----------------
Result : y
----------------

** Case : Character with width specification (4) ******************************

sscanf("tulips,"%4c").
----------------------
Result : tuli
----------------------

** Case : signed floating point with negative exponent ******************************

sscanf("10e-9,"%e").
--------------------
Result : 1.0E-8
--------------------

** Case : signed floating point with explicit positive exponent ******************************

sscanf("10e+9,"%e").
--------------------
Result : 10000000000
--------------------

** Case : signed floating point with positive exponent (no + sign) ******************************

sscanf("10e9,"%e").
-------------------
Result : 10000000000
-------------------

** Case : multiple specifiers ******************************

sscanf(" 19 84 0666 2000 0xface your x 31e+9 0.912 2.4 ," %d %i %o %u %x %s %c %e %f %g ").
-------------------------------------------------------------------------------------------
Result : 19
84
438
2000
64206
your
x
31000000000
0.912
2.4
-------------------------------------------------------------------------------------------