mb_substr_variation1.phpt 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. --TEST--
  2. Test mb_substr() function : usage variations - pass unexpected arguments (including strings) in place of $str
  3. --SKIPIF--
  4. <?php
  5. extension_loaded('mbstring') or die('skip');
  6. function_exists('mb_substr') or die("skip mb_substr() is not available in this build");
  7. ?>
  8. --FILE--
  9. <?php
  10. /* Prototype : string mb_substr(string $str, int $start [, int $length [, string $encoding]])
  11. * Description: Returns part of a string
  12. * Source code: ext/mbstring/mbstring.c
  13. */
  14. /*
  15. * Pass different data types as $str to mb_substr() to test behaviour
  16. */
  17. echo "*** Testing mb_substr() : usage variations ***\n";
  18. // Initialise function arguments not being substituted
  19. $start = 0;
  20. $length = 5;
  21. //get an unset variable
  22. $unset_var = 10;
  23. unset ($unset_var);
  24. // get a class
  25. class classA
  26. {
  27. public function __toString() {
  28. return "Class A object";
  29. }
  30. }
  31. // heredoc string
  32. $heredoc = <<<EOT
  33. hello world
  34. EOT;
  35. // get a resource variable
  36. $fp = fopen(__FILE__, "r");
  37. // unexpected values to be passed to $str argument
  38. $inputs = array(
  39. // int data
  40. /*1*/ 0,
  41. 1,
  42. 12345,
  43. -2345,
  44. // float data
  45. /*5*/ 10.5,
  46. -10.5,
  47. 12.3456789000e10,
  48. 12.3456789000E-10,
  49. .5,
  50. // null data
  51. /*10*/ NULL,
  52. null,
  53. // boolean data
  54. /*12*/ true,
  55. false,
  56. TRUE,
  57. FALSE,
  58. // empty data
  59. /*16*/ "",
  60. '',
  61. // string data
  62. /*18*/ "string",
  63. 'string',
  64. $heredoc,
  65. // object data
  66. /*21*/ new classA(),
  67. // undefined data
  68. /*22*/ @$undefined_var,
  69. // unset data
  70. /*23*/ @$unset_var,
  71. // resource variable
  72. /*24*/ $fp
  73. );
  74. // loop through each element of $inputs to check the behavior of mb_substr()
  75. $iterator = 1;
  76. foreach($inputs as $input) {
  77. echo "\n-- Iteration $iterator --\n";
  78. var_dump( mb_substr($input, $start, $length));
  79. $iterator++;
  80. };
  81. fclose($fp);
  82. echo "Done";
  83. ?>
  84. --EXPECTF--
  85. *** Testing mb_substr() : usage variations ***
  86. -- Iteration 1 --
  87. string(1) "0"
  88. -- Iteration 2 --
  89. string(1) "1"
  90. -- Iteration 3 --
  91. string(5) "12345"
  92. -- Iteration 4 --
  93. string(5) "-2345"
  94. -- Iteration 5 --
  95. string(4) "10.5"
  96. -- Iteration 6 --
  97. string(5) "-10.5"
  98. -- Iteration 7 --
  99. string(5) "12345"
  100. -- Iteration 8 --
  101. string(5) "1.234"
  102. -- Iteration 9 --
  103. string(3) "0.5"
  104. -- Iteration 10 --
  105. string(0) ""
  106. -- Iteration 11 --
  107. string(0) ""
  108. -- Iteration 12 --
  109. string(1) "1"
  110. -- Iteration 13 --
  111. string(0) ""
  112. -- Iteration 14 --
  113. string(1) "1"
  114. -- Iteration 15 --
  115. string(0) ""
  116. -- Iteration 16 --
  117. string(0) ""
  118. -- Iteration 17 --
  119. string(0) ""
  120. -- Iteration 18 --
  121. string(5) "strin"
  122. -- Iteration 19 --
  123. string(5) "strin"
  124. -- Iteration 20 --
  125. string(5) "hello"
  126. -- Iteration 21 --
  127. string(5) "Class"
  128. -- Iteration 22 --
  129. string(0) ""
  130. -- Iteration 23 --
  131. string(0) ""
  132. -- Iteration 24 --
  133. Warning: mb_substr() expects parameter 1 to be string, resource given in %s on line %d
  134. NULL
  135. Done