xref: /PHP-7.1/ext/mbstring/tests/mb_strpos.phpt (revision 7af945e2)
1--TEST--
2mb_strpos()
3--SKIPIF--
4<?php extension_loaded('mbstring') or die('skip mbstring not available'); ?>
5--FILE--
6<?php
7// TODO: Add more encodings
8
9//$debug=true;
10ini_set('include_path','.');
11include_once('common.inc');
12
13
14// Test string
15$euc_jp = b'0123����ʸ��������ܸ�Ǥ���EUC-JP��ȤäƤ��ޤ���0123���ܸ�����ݽ�����';
16
17$slen = mb_strlen($euc_jp, 'EUC-JP');
18echo "String len: $slen\n";
19
20// EUC-JP - With encoding parameter
21mb_internal_encoding('UTF-8') or print("mb_internal_encoding() failed\n");
22
23echo  "== POSITIVE OFFSET ==\n";
24
25print  mb_strpos($euc_jp, b'���ܸ�', 0, 'EUC-JP') . "\n";
26print  mb_strpos($euc_jp, b'0', 0,     'EUC-JP') . "\n";
27print  mb_strpos($euc_jp, 3, 0,       'EUC-JP') . "\n";
28print  mb_strpos($euc_jp, 0, 0,       'EUC-JP') . "\n";
29print  mb_strpos($euc_jp, b'���ܸ�', 15, 'EUC-JP') . "\n";
30print  mb_strpos($euc_jp, b'0', 15,     'EUC-JP') . "\n";
31print  mb_strpos($euc_jp, 3, 15,       'EUC-JP') . "\n";
32print  mb_strpos($euc_jp, 0, 15,       'EUC-JP') . "\n";
33
34
35// Negative offset
36echo "== NEGATIVE OFFSET ==\n";
37
38print mb_strpos($euc_jp, b'���ܸ�', -15, 'EUC-JP') . "\n";
39print mb_strpos($euc_jp, b'0', -15,     'EUC-JP') . "\n";
40print mb_strpos($euc_jp, 3, -15,       'EUC-JP') . "\n";
41print mb_strpos($euc_jp, 0, -15,       'EUC-JP') . "\n";
42print mb_strpos($euc_jp, 0, -43,       'EUC-JP') . "\n";
43
44
45// Invalid offset - should return false with warning
46print ("== INVALID OFFSET ==\n");
47
48$r =  mb_strpos($euc_jp, b'���ܸ�', 44, 'EUC-JP');
49($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
50$r =  mb_strpos($euc_jp, b'���ܸ�', 50, 'EUC-JP');
51($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
52$r =  mb_strpos($euc_jp, b'0', 50,     'EUC-JP');
53($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
54$r =  mb_strpos($euc_jp, 3, 50,       'EUC-JP');
55($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
56$r =   mb_strpos($euc_jp, 0, 50,       'EUC-JP');
57($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
58$r = mb_strpos($euc_jp, b'���ܸ�', -50, 'EUC-JP');
59($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
60$r = mb_strpos($euc_jp, b'0', -50,     'EUC-JP');
61($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
62$r = mb_strpos($euc_jp, 3, -50,       'EUC-JP');
63($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
64$r = mb_strpos($euc_jp, 0, -50,       'EUC-JP');
65($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
66$r = mb_strpos($euc_jp, 0, -44,       'EUC-JP');
67($r === FALSE) ? print "OK_INVALID_OFFSET\n"     : print "NG_INVALID_OFFSET\n";
68
69// Out of range - should return false
70print ("== OUT OF RANGE ==\n");
71
72$r =  mb_strpos($euc_jp, b'���ܸ�', 40, 'EUC-JP');
73($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
74$r =  mb_strpos($euc_jp, b'0', 40,     'EUC-JP');
75($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
76$r =  mb_strpos($euc_jp, 3, 40,       'EUC-JP');
77($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
78$r =   mb_strpos($euc_jp, 0, 40,       'EUC-JP');
79($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
80$r =  mb_strpos($euc_jp, b'���ܸ�', -3, 'EUC-JP');
81($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
82$r =  mb_strpos($euc_jp, b'0', -3,     'EUC-JP');
83($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
84$r =  mb_strpos($euc_jp, 3, -3,       'EUC-JP');
85($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
86$r =   mb_strpos($euc_jp, 0, -3,       'EUC-JP');
87($r === FALSE) ? print "OK_OUT_RANGE\n"     : print "NG_OUT_RANGE\n";
88
89
90// Non-existent
91echo "== NON-EXISTENT ==\n";
92
93$r = mb_strpos($euc_jp, b'�ڹ��', 0, 'EUC-JP');
94($r === FALSE) ? print "OK_STR\n"     : print "NG_STR\n";
95$r = mb_strpos($euc_jp, b"\n",     0, 'EUC-JP');
96($r === FALSE) ? print "OK_NEWLINE\n" : print "NG_NEWLINE\n";
97
98
99// EUC-JP - No encoding parameter
100echo "== NO ENCODING PARAMETER ==\n";
101
102mb_internal_encoding('EUC-JP')  or print("mb_internal_encoding() failed\n");
103
104print  mb_strpos($euc_jp, b'���ܸ�', 0) . "\n";
105print  mb_strpos($euc_jp, b'0', 0) . "\n";
106print  mb_strpos($euc_jp, 3, 0) . "\n";
107print  mb_strpos($euc_jp, 0, 0) . "\n";
108
109$r = mb_strpos($euc_jp, b'�ڹ��', 0);
110($r === FALSE) ? print "OK_STR\n"     : print "NG_STR\n";
111$r = mb_strpos($euc_jp, b"\n", 0);
112($r === FALSE) ? print "OK_NEWLINE\n" : print "NG_NEWLINE\n";
113
114// EUC-JP - No offset and encoding parameter
115echo "== NO OFFSET AND ENCODING PARAMETER ==\n";
116
117mb_internal_encoding('EUC-JP')  or print("mb_internal_encoding() failed\n");
118
119print  mb_strpos($euc_jp, b'���ܸ�') . "\n";
120print  mb_strpos($euc_jp, b'0') . "\n";
121print  mb_strpos($euc_jp, 3) . "\n";
122print  mb_strpos($euc_jp, 0) . "\n";
123
124$r = mb_strpos($euc_jp, b'�ڹ��');
125($r === FALSE) ? print "OK_STR\n"     : print "NG_STR\n";
126$r = mb_strpos($euc_jp, b"\n");
127($r === FALSE) ? print "OK_NEWLINE\n" : print "NG_NEWLINE\n";
128
129
130// Invalid Parameters
131echo "== INVALID PARAMETER TEST ==\n";
132
133$r = mb_strpos($euc_jp,'','EUC-JP');
134($r === NULL) ? print("OK_NULL\n") : print("NG_NULL\n");
135$r = mb_strpos($euc_jp, $t_ary, 'EUC-JP');
136($r === NULL) ? print("OK_ARRAY\n") : print("NG_ARRAY\n");
137$r = mb_strpos($euc_jp, $t_obj, 'EUC-JP');
138($r === NULL) ? print("OK_OBJECT\n") : print("NG_OBJECT\n");
139$r = mb_strpos($euc_jp, $t_obj, 'BAD_ENCODING');
140($r === NULL) ? print("OK_BAD_ENCODING\n") : print("NG_BAD_ENCODING\n");
141?>
142==DONE==
143--EXPECT--
144String len: 43
145== POSITIVE OFFSET ==
14610
1470
1483
1490
15034
15130
15233
15330
154== NEGATIVE OFFSET ==
15534
15630
15733
15830
1590
160== INVALID OFFSET ==
161ERR: Warning
162OK_INVALID_OFFSET
163ERR: Warning
164OK_INVALID_OFFSET
165ERR: Warning
166OK_INVALID_OFFSET
167ERR: Warning
168OK_INVALID_OFFSET
169ERR: Warning
170OK_INVALID_OFFSET
171ERR: Warning
172OK_INVALID_OFFSET
173ERR: Warning
174OK_INVALID_OFFSET
175ERR: Warning
176OK_INVALID_OFFSET
177ERR: Warning
178OK_INVALID_OFFSET
179ERR: Warning
180OK_INVALID_OFFSET
181== OUT OF RANGE ==
182OK_OUT_RANGE
183OK_OUT_RANGE
184OK_OUT_RANGE
185OK_OUT_RANGE
186OK_OUT_RANGE
187OK_OUT_RANGE
188OK_OUT_RANGE
189OK_OUT_RANGE
190== NON-EXISTENT ==
191OK_STR
192OK_NEWLINE
193== NO ENCODING PARAMETER ==
19410
1950
1963
1970
198OK_STR
199OK_NEWLINE
200== NO OFFSET AND ENCODING PARAMETER ==
20110
2020
2033
2040
205OK_STR
206OK_NEWLINE
207== INVALID PARAMETER TEST ==
208ERR: Warning
209OK_NULL
210ERR: Warning
211OK_ARRAY
212ERR: Warning
213OK_OBJECT
214ERR: Warning
215OK_BAD_ENCODING
216==DONE==
217