xref: /PHP-8.0/ext/mbstring/tests/mb_strcut.phpt (revision c37a1cd6)
1--TEST--
2mb_strcut()
3--SKIPIF--
4<?php extension_loaded('mbstring') or die('skip mbstring not available'); ?>
5--INI--
6output_handler=
7--FILE--
8<?php
9function MBStringChars($string, $encoding) {
10  $chars = mb_str_split($string, 1, $encoding);
11  return '[' . implode(' ', array_map(function($char) {
12    return join(unpack('H*', $char));
13  }, $chars)) . ']';
14}
15
16ini_set('include_path', __DIR__);
17include_once('common.inc');
18
19// EUC-JP
20$euc_jp = pack('H*', '30313233a4b3a4cecab8bbfacef3a4cfc6fccbdcb8eca4c7a4b9a1a34555432d4a50a4f2bbc8a4c3a4c6a4a4a4dea4b9a1a3c6fccbdcb8eca4cfccccc5ddbdada4a4a1a3');
21// UTF-8
22$utf8    = pack('H*', 'e288ae2045e28b856461203d2051'); // has 2 multi-byte characters: [e288ae 20 45 e28b85 64 61 20 3d 20 51]
23// UTF-16LE
24$utf16le = pack('H*', '1a043804400438043b043b04380446043004200069007300200043007900720069006c006c0069006300');
25
26print "== EUC-JP ==\n";
27print MBStringChars(mb_strcut($euc_jp,  6,   5,'EUC-JP'), 'EUC-JP') . "\n";
28print MBStringChars(mb_strcut($euc_jp,  5,   5,'EUC-JP'), 'EUC-JP') . "\n";
29print MBStringChars(mb_strcut($euc_jp,  0, 100,'EUC-JP'), 'EUC-JP') . "\n";
30
31$str = mb_strcut($euc_jp, 100, 10,'EUC-JP');
32($str === "") ? print "OK\n" : print "No good\n";
33
34$str = mb_strcut($euc_jp, -100, 10,'EUC-JP');
35($str !== "") ?	print "OK\n" : print "No good\n";
36
37print "== UTF-8 ==\n";
38print MBStringChars(mb_strcut($utf8, 0, 0, 'UTF-8'), 'UTF-8') . "\n";
39print MBStringChars(mb_strcut($utf8, 0, 1, 'UTF-8'), 'UTF-8') . "\n";
40print MBStringChars(mb_strcut($utf8, 0, 2, 'UTF-8'), 'UTF-8') . "\n";
41print MBStringChars(mb_strcut($utf8, 0, 3, 'UTF-8'), 'UTF-8') . "\n";
42print MBStringChars(mb_strcut($utf8, 0, 4, 'UTF-8'), 'UTF-8') . "\n";
43print MBStringChars(mb_strcut($utf8, 0, 5, 'UTF-8'), 'UTF-8') . "\n";
44print MBStringChars(mb_strcut($utf8, 1, 2, 'UTF-8'), 'UTF-8') . "\n";
45print MBStringChars(mb_strcut($utf8, 1, 3, 'UTF-8'), 'UTF-8') . "\n";
46print MBStringChars(mb_strcut($utf8, 1, 4, 'UTF-8'), 'UTF-8') . "\n";
47
48print "== UTF-16LE ==\n";
49print MBStringChars(mb_strcut($utf16le, 0, 0, 'UTF-16LE'), 'UTF-16LE') . "\n";
50print MBStringChars(mb_strcut($utf16le, 0, 1, 'UTF-16LE'), 'UTF-16LE') . "\n";
51print MBStringChars(mb_strcut($utf16le, 0, 2, 'UTF-16LE'), 'UTF-16LE') . "\n";
52print MBStringChars(mb_strcut($utf16le, 0, 3, 'UTF-16LE'), 'UTF-16LE') . "\n";
53print MBStringChars(mb_strcut($utf16le, 1, 2, 'UTF-16LE'), 'UTF-16LE') . "\n";
54print MBStringChars(mb_strcut($utf16le, 1, 3, 'UTF-16LE'), 'UTF-16LE') . "\n";
55print MBStringChars(mb_strcut($utf16le, 1, 4, 'UTF-16LE'), 'UTF-16LE') . "\n";
56
57?>
58--EXPECT--
59== EUC-JP ==
60[a4ce cab8]
61[a4b3 a4ce]
62[30 31 32 33 a4b3 a4ce cab8 bbfa cef3 a4cf c6fc cbdc b8ec a4c7 a4b9 a1a3 45 55 43 2d 4a 50 a4f2 bbc8 a4c3 a4c6 a4a4 a4de a4b9 a1a3 c6fc cbdc b8ec a4cf cccc c5dd bdad a4a4 a1a3]
63OK
64OK
65== UTF-8 ==
66[]
67[]
68[]
69[e288ae]
70[e288ae 20]
71[e288ae 20 45]
72[]
73[e288ae]
74[e288ae 20]
75== UTF-16LE ==
76[]
77[]
78[1a04]
79[1a04]
80[1a04]
81[1a04]
82[1a04 3804]
83