mb_substr_variation4.phpt 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. --TEST--
  2. Test mb_substr() function : usage variations - pass different integers to $start arg
  3. --SKIPIF--
  4. <?php
  5. extension_loaded('mbstring') or die('skip');
  6. function_exists('mb_substr') or die("skip mb_substr() is not available in this build");
  7. ?>
  8. --FILE--
  9. <?php
  10. /* Prototype : string mb_substr(string $str, int $start [, int $length [, string $encoding]])
  11. * Description: Returns part of a string
  12. * Source code: ext/mbstring/mbstring.c
  13. */
  14. /*
  15. * Test how mb_substr() behaves when passed a range of integers as $start argument
  16. */
  17. echo "*** Testing mb_substr() : usage variations ***\n";
  18. mb_internal_encoding('UTF-8');
  19. $string_ascii = b'+Is an English string'; //21 chars
  20. $string_mb = base64_decode('5pel5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII='); //21 chars
  21. /*
  22. * Loop through integers as multiples of ten for $offset argument
  23. * 60 is larger than *BYTE* count for $string_mb
  24. */
  25. for ($i = -60; $i <= 60; $i += 10) {
  26. if (@$a || @$b) {
  27. $a = null;
  28. $b = null;
  29. }
  30. echo "\n**-- Offset is: $i --**\n";
  31. echo "-- ASCII String --\n";
  32. $a = mb_substr($string_ascii, $i, 4);
  33. var_dump(base64_encode($a));
  34. echo "--Multibyte String --\n";
  35. $b = mb_substr($string_mb, $i, 4, 'UTF-8');
  36. if (strlen($a) == mb_strlen($b, 'UTF-8')) { // should return same length
  37. var_dump(base64_encode($b));
  38. } else {
  39. echo "Difference in length of ASCII string and multibyte string\n";
  40. }
  41. }
  42. echo "Done";
  43. ?>
  44. --EXPECT--
  45. *** Testing mb_substr() : usage variations ***
  46. **-- Offset is: -60 --**
  47. -- ASCII String --
  48. string(8) "K0lzIA=="
  49. --Multibyte String --
  50. string(16) "5pel5pys6Kqe44OG"
  51. **-- Offset is: -50 --**
  52. -- ASCII String --
  53. string(8) "K0lzIA=="
  54. --Multibyte String --
  55. string(16) "5pel5pys6Kqe44OG"
  56. **-- Offset is: -40 --**
  57. -- ASCII String --
  58. string(8) "K0lzIA=="
  59. --Multibyte String --
  60. string(16) "5pel5pys6Kqe44OG"
  61. **-- Offset is: -30 --**
  62. -- ASCII String --
  63. string(8) "K0lzIA=="
  64. --Multibyte String --
  65. string(16) "5pel5pys6Kqe44OG"
  66. **-- Offset is: -20 --**
  67. -- ASCII String --
  68. string(8) "SXMgYQ=="
  69. --Multibyte String --
  70. string(16) "5pys6Kqe44OG44Kt"
  71. **-- Offset is: -10 --**
  72. -- ASCII String --
  73. string(8) "aXNoIA=="
  74. --Multibyte String --
  75. string(8) "MTIzNA=="
  76. **-- Offset is: 0 --**
  77. -- ASCII String --
  78. string(8) "K0lzIA=="
  79. --Multibyte String --
  80. string(16) "5pel5pys6Kqe44OG"
  81. **-- Offset is: 10 --**
  82. -- ASCII String --
  83. string(8) "bGlzaA=="
  84. --Multibyte String --
  85. string(8) "MDEyMw=="
  86. **-- Offset is: 20 --**
  87. -- ASCII String --
  88. string(4) "Zw=="
  89. --Multibyte String --
  90. string(4) "44CC"
  91. **-- Offset is: 30 --**
  92. -- ASCII String --
  93. string(0) ""
  94. --Multibyte String --
  95. string(0) ""
  96. **-- Offset is: 40 --**
  97. -- ASCII String --
  98. string(0) ""
  99. --Multibyte String --
  100. string(0) ""
  101. **-- Offset is: 50 --**
  102. -- ASCII String --
  103. string(0) ""
  104. --Multibyte String --
  105. string(0) ""
  106. **-- Offset is: 60 --**
  107. -- ASCII String --
  108. string(0) ""
  109. --Multibyte String --
  110. string(0) ""
  111. Done