Skip to content

Commit d80bcbf

Browse files
authored
Merge pull request #14 from juliangut/fix/unicode-emoji
Fix/unicode emoji
2 parents ccdbf6d + 83f0cfa commit d80bcbf

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

SMSCounter.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -342,14 +342,20 @@ public function utf8ToUnicode($str)
342342
}
343343

344344
if ($thisValue >= 128) {
345-
if (count($values) == 0) {
346-
$lookingFor = ($thisValue < 224) ? 2 : 3;
345+
if (count($values) === 0) {
346+
$lookingFor = 2;
347+
348+
if ($thisValue >= 240) {
349+
$lookingFor = 4;
350+
} elseif ($thisValue >= 224) {
351+
$lookingFor = 3;
352+
}
347353
}
348354

349355
$values[] = $thisValue;
350356

351-
if (count($values) == $lookingFor) {
352-
$number = ($lookingFor == 3) ?
357+
if (count($values) === $lookingFor) {
358+
$number = ($lookingFor === 3) ?
353359
(($values[0] % 16) * 4096) + (($values[1] % 64) * 64) + ($values[2] % 64) :
354360
(($values[0] % 32) * 64) + ($values[1] % 64);
355361

Tests/SMSCounterTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,23 @@ public function testUnicode()
180180
$this->assertEquals($expected, $count);
181181
}
182182

183+
public function testUnicodeEmoji()
184+
{
185+
$text = '😎😎';
186+
187+
$smsCounter = new SMSCounter();
188+
$count = $smsCounter->count($text);
189+
190+
$expected = new \stdClass();
191+
$expected->encoding = SMSCounter::UTF16;
192+
$expected->length = 2;
193+
$expected->per_message = 70;
194+
$expected->remaining = 68;
195+
$expected->messages = 1;
196+
197+
$this->assertEquals($expected, $count);
198+
}
199+
183200
public function testRemoveNonGSMChars()
184201
{
185202
$text = 'áno-unicode-remaining` ñ';

0 commit comments

Comments
 (0)