mb_substr_count_variation3.phpt 4.1 KB

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