xref: /php-src/ext/mbstring/tests/mb_trim.phpt (revision 803cd824)
1--TEST--
2mb_trim() function tests
3--EXTENSIONS--
4mbstring
5--FILE--
6<?php
7mb_internal_encoding("UTF-8");
8
9echo "== Copy from trim ==\n";
10var_dump('ABC' ===  mb_trim('ABC'));
11var_dump('ABC' === mb_ltrim('ABC'));
12var_dump('ABC' === mb_rtrim('ABC'));
13var_dump('ABC' ===  mb_trim(" \0\t\nABC \0\t\n"));
14var_dump("ABC \0\t\n" === mb_ltrim(" \0\t\nABC \0\t\n"));
15var_dump(" \0\t\nABC" === mb_rtrim(" \0\t\nABC \0\t\n"));
16var_dump(" \0\t\nABC \0\t\n" ===  mb_trim(" \0\t\nABC \0\t\n",''));
17var_dump(" \0\t\nABC \0\t\n" === mb_ltrim(" \0\t\nABC \0\t\n",''));
18var_dump(" \0\t\nABC \0\t\n" === mb_rtrim(" \0\t\nABC \0\t\n",''));
19echo "== Empty string ==\n";
20var_dump(mb_trim(""));
21var_dump(mb_ltrim(""));
22var_dump(mb_rtrim(""));
23
24echo "== Single string ==\n";
25var_dump(mb_ltrim(' test ', ''));
26var_dump(mb_trim(" あいうえおあお ", " ", "UTF-8"));
27var_dump(mb_trim('foo BAR Spaß', 'ß', "UTF-8"));
28var_dump(mb_trim('foo BAR Spaß', 'f', "UTF-8"));
29
30echo "== Multi strings ==\n";
31var_dump(mb_trim('foo BAR Spaß', 'ßf', "UTF-8"));
32var_dump(mb_trim('foo BAR Spaß', 'fß', "UTF-8"));
33var_dump(mb_trim(" あいうおえお  あ", " あ", "UTF-8"));
34var_dump(mb_trim(" あいうおえお  あ", "あ ", "UTF-8"));
35var_dump(mb_trim(" あいうおえお  a", "あa", "UTF-8"));
36var_dump(mb_trim(" あいうおえお  a", "\xe3", "UTF-8"));
37
38echo "== Many strings ==\n";
39var_dump(mb_trim(str_repeat(" ", 129)));
40var_dump(mb_trim(str_repeat(" ", 129) . "a"));
41var_dump(mb_rtrim(str_repeat(" ", 129) . "a"));
42
43echo "== Very long trim characters ==\n";
44$trim_chars = "";
45for ($i = 1024; $i < 2048; $i++) {
46    $trim_chars .= mb_chr($i);
47}
48var_dump(mb_trim($trim_chars . "hello" . $trim_chars, $trim_chars));
49var_dump(strlen(mb_ltrim($trim_chars . "hello" . $trim_chars, $trim_chars)));
50var_dump(strlen(mb_rtrim($trim_chars . "hello" . $trim_chars, $trim_chars)));
51
52echo "== mb_ltrim ==\n";
53var_dump(mb_ltrim("あああああああああああああああああああああああああああああああああいああああ", "あ"));
54echo "== mb_rtrim ==\n";
55var_dump(mb_rtrim("あああああああああああああああああああああああああああああああああいああああ", "あ"));
56
57echo "== default params ==\n";
58var_dump(mb_trim(" \f\n\r\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}"));
59
60echo "== Byte Order Mark ==\n";
61var_dump(mb_ltrim("\u{FFFE}漢字", "\u{FFFE}\u{FEFF}"));
62var_dump(bin2hex(mb_ltrim(mb_convert_encoding("\u{FFFE}漢字", "UTF-16LE", "UTF-8"), mb_convert_encoding("\u{FFFE}\u{FEFF}", "UTF-16LE", "UTF-8"), "UTF-16LE")));
63var_dump(bin2hex(mb_ltrim(mb_convert_encoding("\u{FEFF}漢字", "UTF-16BE", "UTF-8"), mb_convert_encoding("\u{FFFE}\u{FEFF}", "UTF-16BE", "UTF-8"), "UTF-16BE")));
64
65echo "== Empty string ==\n";
66var_dump(mb_trim(" abcd ", ""));
67var_dump(mb_ltrim(" abcd ", ""));
68var_dump(mb_rtrim(" abcd ", ""));
69
70echo "== SJIS ==\n";
71var_dump(mb_convert_encoding(mb_trim("\x81\x40\x82\xa0\x81\x40", "\x81\x40", "SJIS"), "UTF-8", "SJIS"));
72
73echo "== Same strings ==\n";
74var_dump(mb_trim("foo", "oo"));
75
76echo "== \$encoding throws ValueError ==\n";
77try {
78    var_dump(mb_trim( "\u{180F}", "", "NULL"));
79} catch (ValueError $e) {
80    var_dump($e->getMessage());
81}
82
83?>
84--EXPECT--
85== Copy from trim ==
86bool(true)
87bool(true)
88bool(true)
89bool(true)
90bool(true)
91bool(true)
92bool(true)
93bool(true)
94bool(true)
95== Empty string ==
96string(0) ""
97string(0) ""
98string(0) ""
99== Single string ==
100string(6) " test "
101string(21) "あいうえおあお"
102string(11) "foo BAR Spa"
103string(12) "oo BAR Spaß"
104== Multi strings ==
105string(10) "oo BAR Spa"
106string(10) "oo BAR Spa"
107string(16) "いうおえお "
108string(16) "いうおえお "
109string(25) " あいうおえお  "
110string(26) " あいうおえお  a"
111== Many strings ==
112string(0) ""
113string(1) "a"
114string(388) "                                                                                                                                 a"
115== Very long trim characters ==
116string(5) "hello"
117int(2053)
118int(2053)
119== mb_ltrim ==
120string(15) "いああああ"
121== mb_rtrim ==
122string(102) "あああああああああああああああああああああああああああああああああい"
123== default params ==
124string(0) ""
125== Byte Order Mark ==
126string(6) "漢字"
127string(8) "226f575b"
128string(8) "6f225b57"
129== Empty string ==
130string(6) " abcd "
131string(6) " abcd "
132string(6) " abcd "
133== SJIS ==
134string(3) "あ"
135== Same strings ==
136string(1) "f"
137== $encoding throws ValueError ==
138string(73) "mb_trim(): Argument #3 ($encoding) must be a valid encoding, "NULL" given"
139