File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed
Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -74,4 +74,26 @@ public static function numberFormat($number, $decimals)
7474 {
7575 return number_format ($ number , $ decimals , '. ' , '' );
7676 }
77+
78+ /**
79+ * @param int $dec
80+ * @link http://stackoverflow.com/a/7153133/2235790
81+ * @author velcrow
82+ */
83+ public static function chr ($ dec )
84+ {
85+ if ($ dec <=0x7F ) {
86+ return chr ($ dec );
87+ }
88+ if ($ dec <=0x7FF ) {
89+ return chr (($ dec >>6 )+192 ).chr (($ dec &63 )+128 );
90+ }
91+ if ($ dec <=0xFFFF ) {
92+ return chr (($ dec >>12 )+224 ).chr ((($ dec >>6 )&63 )+128 ).chr (($ dec &63 )+128 );
93+ }
94+ if ($ dec <=0x1FFFFF ) {
95+ return chr (($ dec >>18 )+240 ).chr ((($ dec >>12 )&63 )+128 ).chr ((($ dec >>6 )&63 )+128 ).chr (($ dec &63 )+128 );
96+ }
97+ return '' ;
98+ }
7799}
Original file line number Diff line number Diff line change @@ -43,4 +43,17 @@ public function testNumberFormat()
4343 $ this ->assertEquals ('2.1 ' , String::numberFormat ('2.12 ' , 1 ));
4444 $ this ->assertEquals ('1234 ' , String::numberFormat (1234 , 1 ));
4545 }
46+
47+ public function testChr ()
48+ {
49+ $ this ->assertEquals ('A ' , String::chr (65 ));
50+ $ this ->assertEquals ('A ' , String::chr (0x41 ));
51+ $ this ->assertEquals ('é ' , String::chr (233 ));
52+ $ this ->assertEquals ('é ' , String::chr (0xE9 ));
53+ $ this ->assertEquals ('⼳ ' , String::chr (12083 ));
54+ $ this ->assertEquals ('⼳ ' , String::chr (0x2F33 ));
55+ $ this ->assertEquals ('🌃 ' , String::chr (127747 ));
56+ $ this ->assertEquals ('🌃 ' , String::chr (0x1F303 ));
57+ $ this ->assertEquals ('' , String::chr (2097152 ));
58+ }
4659}
You can’t perform that action at this time.
0 commit comments