mb_strrchr_variation3.phpt 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. --TEST--
  2. Test mb_strrchr() function : usage variation - different values for part
  3. --SKIPIF--
  4. <?php
  5. extension_loaded('mbstring') or die('skip');
  6. function_exists('mb_strrchr') or die("skip mb_strrchr() is not available in this build");
  7. ?>
  8. --FILE--
  9. <?php
  10. /* Prototype : string mb_strrchr(string haystack, string needle[, bool part[, string encoding]])
  11. * Description: Finds the last occurrence of a character in a string within another
  12. * Source code: ext/mbstring/mbstring.c
  13. * Alias to functions:
  14. */
  15. echo "*** Testing mb_strrchr() : usage variation ***\n";
  16. // Define error handler
  17. function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) {
  18. if (error_reporting() != 0) {
  19. // report non-silenced errors
  20. echo "Error: $err_no - $err_msg, $filename($linenum)\n";
  21. }
  22. }
  23. set_error_handler('test_error_handler');
  24. // Initialise function arguments not being substituted (if any)
  25. $haystack = b'string_val';
  26. $needle = b'_';
  27. $encoding = 'utf-8';
  28. //get an unset variable
  29. $unset_var = 10;
  30. unset ($unset_var);
  31. // define some classes
  32. class classWithToString
  33. {
  34. public function __toString() {
  35. return b"Class A object";
  36. }
  37. }
  38. class classWithoutToString
  39. {
  40. }
  41. // heredoc string
  42. $heredoc = b<<<EOT
  43. hello world
  44. EOT;
  45. // get a resource variable
  46. $fp = fopen(__FILE__, "r");
  47. // add arrays
  48. $index_array = array (1, 2, 3);
  49. $assoc_array = array ('one' => 1, 'two' => 2);
  50. //array of values to iterate over
  51. $inputs = array(
  52. // int data
  53. 'int 0' => 0,
  54. 'int 1' => 1,
  55. 'int 12345' => 12345,
  56. 'int -12345' => -2345,
  57. // float data
  58. 'float 10.5' => 10.5,
  59. 'float -10.5' => -10.5,
  60. 'float 12.3456789000e10' => 12.3456789000e10,
  61. 'float -12.3456789000e10' => -12.3456789000e10,
  62. 'float .5' => .5,
  63. // array data
  64. 'empty array' => array(),
  65. 'int indexed array' => $index_array,
  66. 'associative array' => $assoc_array,
  67. 'nested arrays' => array('foo', $index_array, $assoc_array),
  68. // null data
  69. 'uppercase NULL' => NULL,
  70. 'lowercase null' => null,
  71. // boolean data
  72. 'lowercase true' => true,
  73. 'lowercase false' =>false,
  74. 'uppercase TRUE' =>TRUE,
  75. 'uppercase FALSE' =>FALSE,
  76. // empty data
  77. 'empty string DQ' => "",
  78. 'empty string SQ' => '',
  79. // string data
  80. 'string DQ' => "string",
  81. 'string SQ' => 'string',
  82. 'mixed case string' => "sTrInG",
  83. 'heredoc' => $heredoc,
  84. // object data
  85. 'instance of classWithToString' => new classWithToString(),
  86. 'instance of classWithoutToString' => new classWithoutToString(),
  87. // undefined data
  88. 'undefined var' => @$undefined_var,
  89. // unset data
  90. 'unset var' => @$unset_var,
  91. // resource variable
  92. 'resource' => $fp
  93. );
  94. // loop through each element of the array for part
  95. foreach($inputs as $key =>$value) {
  96. echo "\n--$key--\n";
  97. $res = mb_strrchr($haystack, $needle, $value, $encoding);
  98. if ($res === false) {
  99. var_dump($res);
  100. }
  101. else {
  102. var_dump(bin2hex($res));
  103. }
  104. };
  105. fclose($fp);
  106. ?>
  107. ===DONE===
  108. --EXPECTF--
  109. *** Testing mb_strrchr() : usage variation ***
  110. --int 0--
  111. string(8) "5f76616c"
  112. --int 1--
  113. string(12) "737472696e67"
  114. --int 12345--
  115. string(12) "737472696e67"
  116. --int -12345--
  117. string(12) "737472696e67"
  118. --float 10.5--
  119. string(12) "737472696e67"
  120. --float -10.5--
  121. string(12) "737472696e67"
  122. --float 12.3456789000e10--
  123. string(12) "737472696e67"
  124. --float -12.3456789000e10--
  125. string(12) "737472696e67"
  126. --float .5--
  127. string(12) "737472696e67"
  128. --empty array--
  129. Error: 2 - mb_strrchr() expects parameter 3 to be boolean, array given, %s(%d)
  130. bool(false)
  131. --int indexed array--
  132. Error: 2 - mb_strrchr() expects parameter 3 to be boolean, array given, %s(%d)
  133. bool(false)
  134. --associative array--
  135. Error: 2 - mb_strrchr() expects parameter 3 to be boolean, array given, %s(%d)
  136. bool(false)
  137. --nested arrays--
  138. Error: 2 - mb_strrchr() expects parameter 3 to be boolean, array given, %s(%d)
  139. bool(false)
  140. --uppercase NULL--
  141. string(8) "5f76616c"
  142. --lowercase null--
  143. string(8) "5f76616c"
  144. --lowercase true--
  145. string(12) "737472696e67"
  146. --lowercase false--
  147. string(8) "5f76616c"
  148. --uppercase TRUE--
  149. string(12) "737472696e67"
  150. --uppercase FALSE--
  151. string(8) "5f76616c"
  152. --empty string DQ--
  153. string(8) "5f76616c"
  154. --empty string SQ--
  155. string(8) "5f76616c"
  156. --string DQ--
  157. string(12) "737472696e67"
  158. --string SQ--
  159. string(12) "737472696e67"
  160. --mixed case string--
  161. string(12) "737472696e67"
  162. --heredoc--
  163. string(12) "737472696e67"
  164. --instance of classWithToString--
  165. Error: 2 - mb_strrchr() expects parameter 3 to be boolean, object given, %s(%d)
  166. bool(false)
  167. --instance of classWithoutToString--
  168. Error: 2 - mb_strrchr() expects parameter 3 to be boolean, object given, %s(%d)
  169. bool(false)
  170. --undefined var--
  171. string(8) "5f76616c"
  172. --unset var--
  173. string(8) "5f76616c"
  174. --resource--
  175. Error: 2 - mb_strrchr() expects parameter 3 to be boolean, resource given, %s(%d)
  176. bool(false)
  177. ===DONE===