mb_substr_count_variation2.phpt 3.2 KB

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