4
function print_value($val,$postfix="<br>") {
6
for ($i = 0;$i< count($val);$i++) {
7
echo $val[$i] . $postfix;
14
function do_sscanf($string, $format) {
15
$s = "sscanf(\"" . $string . ",\"" . $format ."\").";
17
$s = str_repeat("-", strlen($s));
19
$output = sscanf($string,$format);
21
print_value( $output );
26
function run_sscanf_test_cases($filename="scan_cases")
29
echo "<h3><em><br>Running Test Cases from $filename<br></em></h3>";
30
$arr = file($filename);
31
for ($i=0;$i < count($arr);$i++) {
32
$line_arr = explode("|",$arr[$i]);
34
$format = $line_arr[0];
35
$string = $line_arr[1];
36
if (count($arr) > 2) {
37
$comment = $line_arr[2];
41
if ( empty($format) || empty($string) ) {
44
print("<h4>** Case : $comment ******************************</h4>");
45
do_sscanf($string,$format);
49
function simple_tests() {
50
echo "Testing sscanf with standard ANSI syntax (values returned by
59
echo "<h3><em><br>Simple Test<br></em></h3>";
60
echo "sscanf('10','%d',&\$decimal) <br>";
61
echo "<br>BEFORE : <br> decimal = $decimal.";
62
$foo = sscanf("10","%d",&$decimal);
63
echo "<br>AFTER : <br> decimal = $decimal <br>";
66
echo "<h3><em><br>Simple Test 2<br></em></h3>";
67
echo "sscanf(\"ghost 0xface\",\"%s %x\",&\$string, &\$int)<br>";
68
echo "<br>BEFORE : <br> string = $string, int = $int<br>";
69
$foo = sscanf("ghost 0xface","%s %x",&$string, &$int);
70
echo "<br>AFTER : <br> string = $string, int = $int<br>";
71
echo " sscan reports : ";
72
print_value( $foo,"");
73
echo " conversions <br>";
75
echo "<h3><em><br>Multiple specifiers<br></em></h3>";
76
echo "sscanf(\"jabberwocky 1024 0xFF 1.024 644 10\",
77
\"%s %d %x %f %o %i\",
78
&\$string,&\$decimal,&\$hex,&\$float,&\$octal,&\$int);<br>";
79
echo "<br>BEFORE : <br>";
80
echo "Decimal = $decimal, String = $string, Hex = $hex<br>";
81
echo "Octal = $octal , Float = $float, Int = $int<br>";
82
$foo = sscanf( "jabberwocky 1024 0xFF 1.024 644 10",
84
&$string,&$decimal,&$hex,&$float,&$octal,&$int);
85
echo "<br>AFTER :<br>";
86
echo "decimal = $decimal, string = $string, hex = $hex<br>";
87
echo "octal = $octal , float = $float, int = $int<br>";
89
echo " sscan reports : ";
90
print_value( $foo,"");
91
echo " conversions <br>";
92
echo "----------------------------------------<br>";
100
<title>Test of sscanf()</title>
103
<strong><h1>Testing sscanf() support in PHP</h1></strong><br>
105
if (!function_exists('sscanf')) {
106
echo "<strong>I'm sorry but sscanf() does not exist !i</strong><br>";
109
run_sscanf_test_cases();