xref: /PHP-8.0/ext/mbstring/tests/mb_strlen.phpt (revision 90eeca25)
1--TEST--
2mb_strlen()
3--SKIPIF--
4<?php extension_loaded('mbstring') or die('skip mbstring not available'); ?>
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 = '0123����ʸ��������ܸ�Ǥ���EUC-JP��ȤäƤ��ޤ���0123���ܸ�����ݽ�����';
17$ascii  = 'abcdefghijklmnopqrstuvwxyz;]=#0123456789';
18
19// ASCII
20echo "== ASCII ==\n";
21print  mb_strlen($ascii,'ASCII') . "\n";
22print  strlen($ascii) . "\n";
23
24// EUC-JP
25echo "== EUC-JP ==\n";
26print  mb_strlen($euc_jp,'EUC-JP') . "\n";
27mb_internal_encoding('EUC-JP') or print("mb_internal_encoding() failed\n");
28print  strlen($euc_jp) . "\n";
29
30// SJIS
31echo "== SJIS ==\n";
32$sjis = mb_convert_encoding($euc_jp, 'SJIS','EUC-JP');
33print  mb_strlen($sjis,'SJIS') . "\n";
34mb_internal_encoding('SJIS') or print("mb_internal_encoding() failed\n");
35print  strlen($sjis) . "\n";
36
37// JIS
38// Note: either convert_encoding or strlen has problem
39echo "== JIS ==\n";
40$jis = mb_convert_encoding($euc_jp, 'JIS','EUC-JP');
41print  mb_strlen($jis,'JIS') . "\n";
42mb_internal_encoding('JIS')  or print("mb_internal_encoding() failed\n");
43print  strlen($jis) . "\n";
44
45// UTF-8
46// Note: either convert_encoding or strlen has problem
47echo "== UTF-8 ==\n";
48$utf8 = mb_convert_encoding($euc_jp, 'UTF-8','EUC-JP');
49print  mb_strlen($utf8,'UTF-8') . "\n";
50mb_internal_encoding('UTF-8')  or print("mb_internal_encoding() failed\n");
51print  strlen($utf8) . "\n";
52
53
54// Wrong Parameters
55echo "== WRONG PARAMETERS ==\n";
56// Wrong encoding
57mb_internal_encoding('EUC-JP');
58try {
59    var_dump( mb_strlen($euc_jp, 'BAD_NAME') );
60} catch (\ValueError $e) {
61    echo $e->getMessage() . \PHP_EOL;
62}
63
64?>
65--EXPECT--
66== ASCII ==
6740
6840
69== EUC-JP ==
7043
7172
72== SJIS ==
7343
7472
75== JIS ==
7643
7790
78== UTF-8 ==
7943
80101
81== WRONG PARAMETERS ==
82mb_strlen(): Argument #2 ($encoding) must be a valid encoding, "BAD_NAME" given
83