mb_strpos_variation3.phpt 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. --TEST--
  2. Test mb_strpos() function : usage variations - pass different data types as $offset arg
  3. --SKIPIF--
  4. <?php
  5. extension_loaded('mbstring') or die('skip');
  6. function_exists('mb_strpos') or die("skip mb_strpos() is not available in this build");
  7. ?>
  8. --FILE--
  9. <?php
  10. /* Prototype : int mb_strpos(string $haystack, string $needle [, int $offset [, string $encoding]])
  11. * Description: Find position of first occurrence of a string within another
  12. * Source code: ext/mbstring/mbstring.c
  13. */
  14. /*
  15. * Pass mb_strpos different data types as $offset arg to test behaviour
  16. */
  17. echo "*** Testing mb_strpos() : usage variations ***\n";
  18. // Initialise function arguments not being substituted
  19. $needle = b'a';
  20. $haystack = b'string_val';
  21. $encoding = 'utf-8';
  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 "Class A object";
  30. }
  31. }
  32. // heredoc string
  33. $heredoc = <<<EOT
  34. hello world
  35. EOT;
  36. // get a resource variable
  37. $fp = fopen(__FILE__, "r");
  38. // unexpected values to be passed to $offest 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*/ "string",
  64. 'string',
  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_strpos()
  76. $iterator = 1;
  77. foreach($inputs as $input) {
  78. echo "\n-- Iteration $iterator --\n";
  79. var_dump( mb_strpos($haystack, $needle, $input, $encoding));
  80. $iterator++;
  81. };
  82. fclose($fp);
  83. echo "Done";
  84. ?>
  85. --EXPECTF--
  86. *** Testing mb_strpos() : usage variations ***
  87. -- Iteration 1 --
  88. int(8)
  89. -- Iteration 2 --
  90. int(8)
  91. -- Iteration 3 --
  92. Warning: mb_strpos(): Offset not contained in string in %s on line %d
  93. bool(false)
  94. -- Iteration 4 --
  95. Warning: mb_strpos(): Offset not contained in string in %s on line %d
  96. bool(false)
  97. -- Iteration 5 --
  98. bool(false)
  99. -- Iteration 6 --
  100. Warning: mb_strpos(): Offset not contained in string in %s on line %d
  101. bool(false)
  102. -- Iteration 7 --
  103. Warning: mb_strpos(): Offset not contained in string in %s on line %d
  104. bool(false)
  105. -- Iteration 8 --
  106. int(8)
  107. -- Iteration 9 --
  108. int(8)
  109. -- Iteration 10 --
  110. int(8)
  111. -- Iteration 11 --
  112. int(8)
  113. -- Iteration 12 --
  114. int(8)
  115. -- Iteration 13 --
  116. int(8)
  117. -- Iteration 14 --
  118. int(8)
  119. -- Iteration 15 --
  120. int(8)
  121. -- Iteration 16 --
  122. Warning: mb_strpos() expects parameter 3 to be long, string given in %s on line %d
  123. bool(false)
  124. -- Iteration 17 --
  125. Warning: mb_strpos() expects parameter 3 to be long, string given in %s on line %d
  126. bool(false)
  127. -- Iteration 18 --
  128. Warning: mb_strpos() expects parameter 3 to be long, string given in %s on line %d
  129. bool(false)
  130. -- Iteration 19 --
  131. Warning: mb_strpos() expects parameter 3 to be long, string given in %s on line %d
  132. bool(false)
  133. -- Iteration 20 --
  134. Warning: mb_strpos() expects parameter 3 to be long, string given in %s on line %d
  135. bool(false)
  136. -- Iteration 21 --
  137. Warning: mb_strpos() expects parameter 3 to be long, object given in %s on line %d
  138. bool(false)
  139. -- Iteration 22 --
  140. int(8)
  141. -- Iteration 23 --
  142. int(8)
  143. -- Iteration 24 --
  144. Warning: mb_strpos() expects parameter 3 to be long, resource given in %s on line %d
  145. bool(false)
  146. Done