Skip to content

Commit 8b8ae85

Browse files
committed
Merge pull request #3 from PHPOffice/develop
Version 0.1.1
2 parents 43d19c8 + 2901d27 commit 8b8ae85

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/Common/String.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

tests/Common/Tests/StringTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)