sscanfPARSES input from a string according to a format.
This function has the input analog of the function printf.
Any whitespace in the format string matches any whitespace in the input string.
This means that even a tab, ( \t ) in the format string can match a single space character in the input string.
Undue substitutions will produce unpredictable results.
If only two parameters were passed to this function, the values parsed will be returned as an array.
Otherwise, if optional parameters are passed, the function will return the number of assigned values.
If there are more substrings expected in the format than there are available within $str, -1 will be returned.
<?php
mix sscanf ( str $str , str $format [,
mix &$arg1, ..., mix &$argN ] )
where,
$str = The input string being parsed.
$format = STRING FORMAT, with internal,
% replaceable specifiers
( SEE the below TABLE )
$arg1, ... , $argN = PARSED ARGUMENTS passed by reference
( Must contain the parsed values )
?>
$str
The input string being parsed.
$format
This function
sscanf is not LOCALE-AWARE.
Each SPECIFIER, present in FORMATTED STRING, must have its own SUBSTITUTE ARGUMENT.
SPECIFIER % |
TYPE |
ARGUMENT |
COMMENT |
%% |
|
literally % |
|
%b |
INTEGER |
BINARY |
NOT SUPPORTED |
%c |
INTEGER |
CHARACTER - ASCII |
|
%d |
INTEGER |
DECIMAL NUMBER |
+/- |
%D |
INTEGER |
DECIMAL NUMBER |
+/- |
%e |
FLOAT |
SCIENTIFIC NOTATION |
|
%E |
FLOAT |
As %e with the use of UPPERCASE
|
|
%u |
INTEGER |
UNSIGNED DECIMAL number. |
no signal |
%f |
FLOAT |
Compatible with LOCALE |
NOT SUPPORTED |
%F |
FLOAT |
As %f not compatible with LOCALE |
NOT SUPPORTED |
%g |
FLOAT |
Shorter of %e and %f. |
NOT SUPPORTED |
%G |
FLOAT |
Shorter of %E e %F |
NOT SUPPORTED |
%i |
INTEGER |
INTEGER number with base detection |
|
%n |
INTEGER |
NUMBER of characters processed so far |
|
%o |
INTEGER |
OCTAL number |
|
%s |
STRING |
STOPS reading at any whitespace character
|
|
%x |
INTEGER |
HEXADECIMAL number |
lowercase |
%X |
INTEGER |
HEXADECIMAL number |
UPERCASE |
ADDITIONAL FORMATS |
0 |
ZERO |
FILLING WITH ZERO |
Number of desired decimal places |
" " |
SPACE |
FILLING WITH SPACES |
Number of desired places |
- |
HYPHEN |
Left-aligned value |
Right-aligned as default |
N1.N2 |
N1 |
INTEGER
|
NUMBER OF DIGITS |
N2 |
DECIMAL |
NUMBER OF DIGITS |
ed48 |
&$arg1 ... &$argN
Parsed arguments, passed by reference.
EXERCISE
<?php
$min01 = PHP_INT_MIN;
$max01 = PHP_INT_MAX;
$arg1 = mt_rand($min01, $max01);
$strA = 'Randon number ' . $arg1;
$sc01 = sscanf($strA, '%s%s%u', $str1, $str2, $nbr1);
// $str1, $str2 and $nbr1 are passed by reference
echo $str1 . ' ' . $str2 . ' = ' . $nbr1;
?>
EXERCISE
<?php
echo "Testing sscanf() :<br>
basic functionality -
using string format.<br><br>";
$str = "Part: Widget Serial Number: 1234789 Stock: 25";
$format = "Part: %s Serial Number: %s Stock: %s";
echo "<br>Try sccanf() WITHOUT optional args:<br>";
// extract details using short format
list($part, $number, $stock) = sscanf($str, $format);
var_dump($part, $number, $stock);
echo "<br><br>Try sccanf() WITH optional args:<br>";
// extract details using long format
$res = sscanf($str, $format, $part, $number, $stock);
var_dump($res, $part, $number, $stock);
?>
EXERCISE
<?php
echo "Testing sscanf() :<br>
basic functionality -
using integer format.<br>";
$str = "Part: Widget Serial Number: 1234789 Stock: 25";
$format = "Part: %s Serial Number: %d Stock: %d";
echo "<br><br>Try sccanf() WITHOUT optional args:<br>";
// extract details using short format
list($part, $number, $stock) = sscanf($str, $format);
var_dump($part, $number, $stock);
echo "<br><br><br>Try sccanf() WITH optional args:<br>";
// extract details using long format
$res = sscanf($str, $format, $part, $number, $stock);
var_dump($res, $part, $number, $stock);
?>
EXERCISE
<?php
echo "Testing sscanf() :<br>
basic functionality -
using float format.<br><br>";
$str = "Part: Widget Length: 111.53 Width: 22.345 Depth: 12.4";
$format = "Part: %s Length: %f Width: %f Depth: %f";
echo "<br>Try sccanf() WITHOUT optional args:<br>";
// extract details using short format
list($part, $length, $width, $depth) = sscanf($str, $format);
var_dump($part, $length, $width, $depth);
echo "<br><br><br>Try sccanf() WITH optional args:<br>";
// extract details using long format
$res = sscanf($str, $format, $part, $length, $width, $depth);
var_dump($res, $part, $length, $width, $depth);
?>
EXERCISE
<?php
echo "Testing sscanf() :<br>
basic functionality -
using char format.<br><br>";
$str = "X = A + B - C";
$format = "%c = %c + %c - %c";
echo "<br>Try sccanf() WITHOUT optional args:<br>";
// extract details using short format
list($arg1, $arg2, $arg3, $arg4) = sscanf($str, $format);
var_dump($arg1, $arg2, $arg3, $arg4);
echo "<br><br><br>Try sccanf() WITH optional args:<br>";
// extract details using long format
$res = sscanf($str, $format, $arg1, $arg2, $arg3, $arg4);
var_dump($res, $arg1, $arg2, $arg3, $arg4);
?>
EXERCISE
<?php
echo "Testing sscanf() :<br>
basic functionality -
using exponential format.<br><br>";
$str = "10.12345 10.12345E3 10.12345e3 -10.12345e4" ;
$format1 = "%e %e %e %e";
$format2 = "%E %E %E %E";
echo "<br>Try sccanf() WITHOUT optional args:<br>";
// extract details using short format
list($arg1, $arg2, $arg3, $arg4) = sscanf($str, $format1);
var_dump($arg1, $arg2, $arg3, $arg4);
echo '<br><br>';
list($arg1, $arg2, $arg3, $arg4) = sscanf($str, $format2);
var_dump($arg1, $arg2, $arg3, $arg4);
echo "<br><br><br>Try sccanf() WITH optional args:<br>";
// extract details using long format
$res = sscanf($str, $format1, $arg1, $arg2, $arg3, $arg4);
var_dump($res, $arg1, $arg2, $arg3, $arg4);
echo '<br><br>';
$res = sscanf($str, $format2,$arg1, $arg2, $arg3, $arg4);
var_dump($res, $arg1, $arg2, $arg3, $arg4);
?>
EXERCISE
<?php
echo "Testing sscanf() :<br>
basic functionality -
using unsigned format.<br><br>";
$str = "-11 +11 11 -11.234 +11.234 11.234";
$format = "%u %u %u %u %u %u";
echo "<br>Try sccanf() WITHOUT optional args:<br>";
// extract details using short format
list($arg1, $arg2, $arg3, $arg4, $arg5, $arg6) =
sscanf($str, $format);
var_dump($arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
echo "<br><br><br>Try sccanf() WITH optional args:<br>";
// extract details using long format
$res = sscanf($str, $format, $arg1, $arg2,
$arg3, $arg4, $arg5, $arg6);
var_dump($res, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
?>
EXERCISE
<?php
echo "Testing sscanf() :<br>
basic functionality -
using octal format.<br><br>";
$str = "0123 -0123 +0123 0129 -0129 +0129";
$format = "%o %o %o %o %o %o";
echo "<br>Try sccanf() WITHOUT optional args:<br>";
// extract details using short format
list($arg1, $arg2, $arg3, $arg4, $arg5, $arg6) =
sscanf($str, $format);
var_dump($arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
echo "<br><br><br>Try sccanf() WITH optional args:<br>";
// extract details using long format
$res = sscanf($str, $format, $arg1, $arg2,
$arg3, $arg4, $arg5, $arg6);
var_dump($res, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
?>
EXERCISE
<?php
echo "Testing sscanf() :<br>
basic functionality -
using hexadecimal format.<br><br>";
$str = "129 12F 123B -123B 01ABC 1G";
$format1 = "%x %x %x %x %x %x";
$format2 = "%X %X %X %X %X %X";
echo "<br>Try sccanf() WITHOUT optional args:<br>";
// extract details using short format
list($arg1, $arg2, $arg3, $arg4, $arg5, $arg6) =
sscanf($str, $format1);
var_dump($arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
list($arg1, $arg2, $arg3, $arg4, $arg5, $arg6) =
sscanf($str, $format2);
echo '<br><br>';
var_dump($arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
echo "<br><br>br>Try sccanf() WITH optional args:<br>";
// extract details using long format
$res = sscanf($str, $format1, $arg1, $arg2,
$arg3, $arg4, $arg5, $arg6);
var_dump($res, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
$res = sscanf($str, $format2, $arg1,
$arg2, $arg3, $arg4, $arg5, $arg6);
echo '<br><br>';
var_dump($res, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
?>
EXERCISE
<?php
echo "Testing sscanf() : error conditions.<br><br>";
$str = "Hello World";
$format = "%s %s";
echo "<br>Testing sscanf() function
with more than expected number of arguments:<br>";
try {
sscanf($str, $format, $str1, $str2, $extra_str);
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
?>