mb_strpos_variation5.phpt 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. --TEST--
  2. Test mb_strpos() function : usage variations - Pass different integers as $offset argument
  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. * Test how mb_strpos() behaves when passed different integers as $offset argument
  16. * The character length of $string_ascii and $string_mb is the same,
  17. * and the needle appears at the same positions in both strings
  18. */
  19. mb_internal_encoding('UTF-8');
  20. echo "*** Testing mb_strpos() : usage variations ***\n";
  21. $string_ascii = b'+Is an English string'; //21 chars
  22. $needle_ascii = b'g';
  23. $string_mb = base64_decode('5pel5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII='); //21 chars
  24. $needle_mb = base64_decode('44CC');
  25. /*
  26. * Loop through integers as multiples of ten for $offset argument
  27. * mb_strpos should not be able to accept negative values as $offset.
  28. * 60 is larger than *BYTE* count for $string_mb
  29. */
  30. for ($i = -10; $i <= 60; $i += 10) {
  31. echo "\n**-- Offset is: $i --**\n";
  32. echo "-- ASCII String --\n";
  33. var_dump(mb_strpos($string_ascii, $needle_ascii, $i));
  34. echo "--Multibyte String --\n";
  35. var_dump(mb_strpos($string_mb, $needle_mb, $i, 'UTF-8'));
  36. }
  37. echo "Done";
  38. ?>
  39. --EXPECTF--
  40. *** Testing mb_strpos() : usage variations ***
  41. **-- Offset is: -10 --**
  42. -- ASCII String --
  43. Warning: mb_strpos(): Offset not contained in string in %s on line %d
  44. bool(false)
  45. --Multibyte String --
  46. Warning: mb_strpos(): Offset not contained in string in %s on line %d
  47. bool(false)
  48. **-- Offset is: 0 --**
  49. -- ASCII String --
  50. int(9)
  51. --Multibyte String --
  52. int(9)
  53. **-- Offset is: 10 --**
  54. -- ASCII String --
  55. int(20)
  56. --Multibyte String --
  57. int(20)
  58. **-- Offset is: 20 --**
  59. -- ASCII String --
  60. int(20)
  61. --Multibyte String --
  62. int(20)
  63. **-- Offset is: 30 --**
  64. -- ASCII String --
  65. Warning: mb_strpos(): Offset not contained in string in %s on line %d
  66. bool(false)
  67. --Multibyte String --
  68. Warning: mb_strpos(): Offset not contained in string in %s on line %d
  69. bool(false)
  70. **-- Offset is: 40 --**
  71. -- ASCII String --
  72. Warning: mb_strpos(): Offset not contained in string in %s on line %d
  73. bool(false)
  74. --Multibyte String --
  75. Warning: mb_strpos(): Offset not contained in string in %s on line %d
  76. bool(false)
  77. **-- Offset is: 50 --**
  78. -- ASCII String --
  79. Warning: mb_strpos(): Offset not contained in string in %s on line %d
  80. bool(false)
  81. --Multibyte String --
  82. Warning: mb_strpos(): Offset not contained in string in %s on line %d
  83. bool(false)
  84. **-- Offset is: 60 --**
  85. -- ASCII String --
  86. Warning: mb_strpos(): Offset not contained in string in %s on line %d
  87. bool(false)
  88. --Multibyte String --
  89. Warning: mb_strpos(): Offset not contained in string in %s on line %d
  90. bool(false)
  91. Done