mb_strlen.phpt 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. --TEST--
  2. mb_strlen()
  3. --SKIPIF--
  4. <?php extension_loaded('mbstring') or die('skip mbstring not available'); ?>
  5. --INI--
  6. mbstring.func_overload=0
  7. --FILE--
  8. <?php
  9. // TODO: Add more encodings
  10. //$debug=true;
  11. ini_set('include_path', dirname(__FILE__));
  12. include_once('common.inc');
  13. // restore detect_order to 'auto'
  14. mb_detect_order('auto');
  15. // Test string
  16. $euc_jp = '0123この文字列は日本語です。EUC-JPを使っています。0123日本語は面倒臭い。';
  17. $ascii = 'abcdefghijklmnopqrstuvwxyz;]=#0123456789';
  18. // ASCII
  19. echo "== ASCII ==\n";
  20. print mb_strlen($ascii,'ASCII') . "\n";
  21. print strlen($ascii) . "\n";
  22. // EUC-JP
  23. echo "== EUC-JP ==\n";
  24. print mb_strlen($euc_jp,'EUC-JP') . "\n";
  25. mb_internal_encoding('EUC-JP') or print("mb_internal_encoding() failed\n");
  26. print strlen($euc_jp) . "\n";
  27. // SJIS
  28. echo "== SJIS ==\n";
  29. $sjis = mb_convert_encoding($euc_jp, 'SJIS','EUC-JP');
  30. print mb_strlen($sjis,'SJIS') . "\n";
  31. mb_internal_encoding('SJIS') or print("mb_internal_encoding() failed\n");
  32. print strlen($sjis) . "\n";
  33. // JIS
  34. // Note: either convert_encoding or strlen has problem
  35. echo "== JIS ==\n";
  36. $jis = mb_convert_encoding($euc_jp, 'JIS','EUC-JP');
  37. print mb_strlen($jis,'JIS') . "\n";
  38. mb_internal_encoding('JIS') or print("mb_internal_encoding() failed\n");
  39. print strlen($jis) . "\n";
  40. // UTF-8
  41. // Note: either convert_encoding or strlen has problem
  42. echo "== UTF-8 ==\n";
  43. $utf8 = mb_convert_encoding($euc_jp, 'UTF-8','EUC-JP');
  44. print mb_strlen($utf8,'UTF-8') . "\n";
  45. mb_internal_encoding('UTF-8') or print("mb_internal_encoding() failed\n");
  46. print strlen($utf8) . "\n";
  47. // Wrong Parameters
  48. echo "== WRONG PARAMETERS ==\n";
  49. // Array
  50. // Note: PHP Warning, strlen() expects parameter 1 to be string, array given
  51. $r = strlen($t_ary);
  52. echo $r."\n";
  53. // Object
  54. // Note: PHP Warning, strlen() expects parameter 1 to be string, object given
  55. $r = strlen($t_obj);
  56. echo $r."\n";
  57. // Wrong encoding
  58. mb_internal_encoding('EUC-JP');
  59. $r = mb_strlen($euc_jp, 'BAD_NAME');
  60. echo $r."\n";
  61. ?>
  62. --EXPECT--
  63. == ASCII ==
  64. 40
  65. 40
  66. == EUC-JP ==
  67. 43
  68. 72
  69. == SJIS ==
  70. 43
  71. 72
  72. == JIS ==
  73. 43
  74. 90
  75. == UTF-8 ==
  76. 43
  77. 101
  78. == WRONG PARAMETERS ==
  79. ERR: Warning
  80. ERR: Warning
  81. ERR: Warning