You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.8 KiB

  1. <?php
  2. function print_value($val,$postfix="<br>") {
  3. if (is_array($val)) {
  4. for ($i = 0;$i< count($val);$i++) {
  5. echo $val[$i] . $postfix;
  6. }
  7. } else {
  8. echo $val . $postfix;
  9. }
  10. }
  11. function do_sscanf($string, $format) {
  12. $s = "sscanf(\"" . $string . ",\"" . $format ."\").";
  13. echo "$s<br>";
  14. $s = str_repeat("-", strlen($s));
  15. echo "$s<br>";
  16. $output = sscanf($string,$format);
  17. echo "Result : ";
  18. print_value( $output );
  19. echo "$s<br><br>";
  20. }
  21. function run_sscanf_test_cases($filename="scan_cases")
  22. {
  23. echo "<h3><em><br>Running Test Cases from $filename<br></em></h3>";
  24. $arr = file($filename);
  25. for ($i=0;$i < count($arr);$i++) {
  26. $line_arr = explode("|",$arr[$i]);
  27. $format = $line_arr[0];
  28. $string = $line_arr[1];
  29. if (count($arr) > 2) {
  30. $comment = $line_arr[2];
  31. } else {
  32. $comment = "";
  33. }
  34. if ( empty($format) || empty($string) ) {
  35. continue;
  36. }
  37. print("<h4>** Case : $comment ******************************</h4>");
  38. do_sscanf($string,$format);
  39. }
  40. }
  41. function simple_tests() {
  42. echo "Testing sscanf with standard ANSI syntax (values returned by
  43. reference)-<br>";
  44. $decimal = -1;
  45. $string = "";
  46. $hex = 0;
  47. $float = 0.0;
  48. $octal = 0.0;
  49. $int = -1;
  50. echo "<h3><em><br>Simple Test<br></em></h3>";
  51. echo "sscanf('10','%d',&\$decimal) <br>";
  52. echo "<br>BEFORE : <br> decimal = $decimal.";
  53. $foo = sscanf("10","%d",&$decimal);
  54. echo "<br>AFTER : <br> decimal = $decimal <br>";
  55. echo "<h3><em><br>Simple Test 2<br></em></h3>";
  56. echo "sscanf(\"ghost 0xface\",\"%s %x\",&\$string, &\$int)<br>";
  57. echo "<br>BEFORE : <br> string = $string, int = $int<br>";
  58. $foo = sscanf("ghost 0xface","%s %x",&$string, &$int);
  59. echo "<br>AFTER : <br> string = $string, int = $int<br>";
  60. echo " sscan reports : ";
  61. print_value( $foo,"");
  62. echo " conversions <br>";
  63. echo "<h3><em><br>Multiple specifiers<br></em></h3>";
  64. echo "sscanf(\"jabberwocky 1024 0xFF 1.024 644 10\",
  65. \"%s %d %x %f %o %i\",
  66. &\$string,&\$decimal,&\$hex,&\$float,&\$octal,&\$int);<br>";
  67. echo "<br>BEFORE : <br>";
  68. echo "Decimal = $decimal, String = $string, Hex = $hex<br>";
  69. echo "Octal = $octal , Float = $float, Int = $int<br>";
  70. $foo = sscanf( "jabberwocky 1024 0xFF 1.024 644 10",
  71. "%s %d %x %f %o %i",
  72. &$string,&$decimal,&$hex,&$float,&$octal,&$int);
  73. echo "<br>AFTER :<br>";
  74. echo "decimal = $decimal, string = $string, hex = $hex<br>";
  75. echo "octal = $octal , float = $float, int = $int<br>";
  76. echo " sscan reports : ";
  77. print_value( $foo,"");
  78. echo " conversions <br>";
  79. echo "----------------------------------------<br>";
  80. }
  81. ?>
  82. <html>
  83. <head>
  84. <title>Test of sscanf()</title>
  85. </head>
  86. <body>
  87. <strong><h1>Testing sscanf() support in PHP</h1></strong><br>
  88. <?php
  89. if (!function_exists('sscanf')) {
  90. echo "<strong>I'm sorry but sscanf() does not exist !i</strong><br>";
  91. } else {
  92. simple_tests();
  93. run_sscanf_test_cases();
  94. }
  95. ?>
  96. </body>
  97. </html>