xref: /PHP-8.1/ext/mbstring/tests/mb_strlen.phpt (revision c50172e8)
1--TEST--
2mb_strlen()
3--EXTENSIONS--
4mbstring
5--FILE--
6<?php
7// TODO: Add more encodings
8
9ini_set('include_path', __DIR__);
10include_once('common.inc');
11
12// restore detect_order to 'auto'
13mb_detect_order('auto');
14
15// Test string
16$euc_jp = mb_convert_encoding("0123この文字列は日本語です。EUC-JPを使っています。0123日本語は面倒臭い。", 'EUC-JP', 'UTF-8');
17$ascii  = 'abcdefghijklmnopqrstuvwxyz;]=#0123456789';
18
19echo "== ASCII ==\n";
20print mb_strlen($ascii,'ASCII') . "\n";
21print strlen($ascii) . "\n";
22
23echo "== EUC-JP ==\n";
24print mb_strlen($euc_jp,'EUC-JP') . "\n";
25mb_internal_encoding('EUC-JP') or print("mb_internal_encoding() failed\n");
26print strlen($euc_jp) . "\n";
27
28echo "== SJIS ==\n";
29$sjis = mb_convert_encoding($euc_jp, 'SJIS','EUC-JP');
30print mb_strlen($sjis,'SJIS') . "\n";
31mb_internal_encoding('SJIS') or print("mb_internal_encoding() failed\n");
32print strlen($sjis) . "\n";
33print "-- Testing illegal bytes 0x80,0xFD-FF --\n";
34// mb_strlen used to wrongly treat 0x80 as the starting byte of a 2-byte SJIS character
35print mb_strlen("\x80\xA1", 'SJIS') . "\n";
36print mb_strlen("abc\xFD\xFE\xFF", 'SJIS') . "\n";
37
38echo "== CP932 ==\n";
39print mb_strlen("\x80\xA1", "CP932") . "\n";
40// 0xFD, 0xFE, 0xFF is reserved.
41print mb_strlen("abc\xFD\xFE\xFF", 'CP932') . "\n";
42
43echo "== MacJapanese ==\n";
44print mb_strlen("\x80\xA1", 'MacJapanese') . "\n";
45print mb_strlen("abc\xFD\xFE\xFF", 'MacJapanese') . "\n";
46
47echo "== SJIS-2004 ==\n";
48print mb_strlen("\x80\xA1", 'SJIS-2004') . "\n";
49print mb_strlen("abc\xFD\xFE\xFF", 'SJIS-2004') . "\n";
50
51echo "== SJIS-Mobile#DOCOMO ==\n";
52print mb_strlen("\x80\xA1", 'SJIS-Mobile#DOCOMO') . "\n";
53print mb_strlen("abc\xFD\xFE\xFF", 'SJIS-Mobile#DOCOMO') . "\n";
54
55echo "== SJIS-Mobile#KDDI ==\n";
56print mb_strlen("\x80\xA1", 'SJIS-Mobile#KDDI') . "\n";
57print mb_strlen("abc\xFD\xFE\xFF", 'SJIS-Mobile#KDDI') . "\n";
58
59echo "== SJIS-Mobile#SoftBank ==\n";
60print mb_strlen("\x80\xA1", 'SJIS-Mobile#SoftBank') . "\n";
61print mb_strlen("abc\xFD\xFE\xFF", 'SJIS-Mobile#SoftBank') . "\n";
62
63echo "== JIS ==\n";
64$jis = mb_convert_encoding($euc_jp, 'JIS','EUC-JP');
65print mb_strlen($jis,'JIS') . "\n";
66mb_internal_encoding('JIS')  or print("mb_internal_encoding() failed\n");
67print strlen($jis) . "\n";
68
69echo "== UTF-8 ==\n";
70$utf8 = mb_convert_encoding($euc_jp, 'UTF-8','EUC-JP');
71print mb_strlen($utf8,'UTF-8') . "\n";
72mb_internal_encoding('UTF-8')  or print("mb_internal_encoding() failed\n");
73print strlen($utf8) . "\n";
74
75// Wrong Parameters
76echo "== WRONG PARAMETERS ==\n";
77// Wrong encoding
78mb_internal_encoding('EUC-JP');
79try {
80    var_dump( mb_strlen($euc_jp, 'BAD_NAME') );
81} catch (\ValueError $e) {
82    echo $e->getMessage() . \PHP_EOL;
83}
84
85?>
86--EXPECT--
87== ASCII ==
8840
8940
90== EUC-JP ==
9143
9272
93== SJIS ==
9443
9572
96-- Testing illegal bytes 0x80,0xFD-FF --
972
986
99== CP932 ==
1002
1015
102== MacJapanese ==
1032
1046
105== SJIS-2004 ==
1062
1076
108== SJIS-Mobile#DOCOMO ==
1092
1106
111== SJIS-Mobile#KDDI ==
1122
1136
114== SJIS-Mobile#SoftBank ==
1152
1166
117== JIS ==
11843
11990
120== UTF-8 ==
12143
122101
123== WRONG PARAMETERS ==
124mb_strlen(): Argument #2 ($encoding) must be a valid encoding, "BAD_NAME" given
125