mb_substr_variation7.phpt 3.3 KB

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