Skip to content
This repository has been archived by the owner on Jul 6, 2021. It is now read-only.

Commit

Permalink
Merge pull request #7 from carlbergman/support_for_surrogate_pairs
Browse files Browse the repository at this point in the history
Added support for surrogate pairs.
  • Loading branch information
acpmasquerade authored Aug 8, 2016
2 parents cd42eaa + eb32cc6 commit fcd8546
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 17 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ language: php
php:
# using major version aliases

# aliased to 5.2.17
- 5.2
# aliased to 5.3.29
- 5.3
# aliased to a recent 5.4.x version
Expand Down
75 changes: 60 additions & 15 deletions lib/SMSCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,30 @@ public static function count($text){
$ex_chars = array();
$encoding = self::detect_encoding($unicode_array, $ex_chars);

$length = count($unicode_array);
if ($encoding === self::UTF16) {

if ( $encoding === self::GSM_7BIT_EX){
$length_exchars = count($ex_chars);
# Each exchar in the GSM 7 Bit encoding takes one more space
# Hence the length increases by one char for each of those Ex chars.
$length += $length_exchars;
}
$length = 0;

foreach($unicode_array as $uc) {

// UTF-16 stores most characters as two bytes,
// but it can only store 0xFFFF (= 65535) characters this way.
// Characters above that number are stored as four bytes and
// therefore need to count as 2 in length in a text message.
$length += ($uc > 65535) ? 2 : 1;

}

} else {
$length = count($unicode_array);

if ( $encoding === self::GSM_7BIT_EX){
$length_exchars = count($ex_chars);
# Each exchar in the GSM 7 Bit encoding takes one more space
# Hence the length increases by one char for each of those Ex chars.
$length += $length_exchars;
}
}

# Select the per message length according to encoding and the message length
switch($encoding){
Expand Down Expand Up @@ -126,7 +142,7 @@ public static function count($text){
}

$messages = ceil($length / $per_message);
$remaining = ( $per_message * $messages ) - $length ;
$remaining = ( $per_message * $messages ) - $length;

$returnset = new stdClass();

Expand Down Expand Up @@ -182,18 +198,47 @@ public static function utf8_to_unicode( $str ) {

$this_value = ord( $str[ $i ] );

if ( $this_value < 128 ) $unicode[] = $this_value;
else {

if ( count( $values ) == 0 ) $looking_for = ( $this_value < 224 ) ? 2 : 3;
if ( $this_value < 128 ) {

$unicode[] = $this_value;

} else {

if ( count( $values ) == 0 ) {

if ($this_value < 224) {
$looking_for = 2;
} else if ($this_value < 240) {
$looking_for = 3;
} else if ($this_value < 248) {
$looking_for = 4;
}

}

$values[] = $this_value;

if ( count( $values ) == $looking_for ) {

$number = ( $looking_for == 3 ) ?
( ( $values[0] % 16 ) * 4096 ) + ( ( $values[1] % 64 ) * 64 ) + ( $values[2] % 64 ):
( ( $values[0] % 32 ) * 64 ) + ( $values[1] % 64 );
if ($looking_for == 4) {

$number = ( ( $values[0] % 8 ) * 262144 ) +
( ( $values[1] % 64 ) * 4096 ) +
( ( $values[2] % 64 ) * 64 ) +
( $values[3] % 64 );

} else if ($looking_for == 3) {

$number = ( ( $values[0] % 16 ) * 4096 ) +
( ( $values[1] % 64 ) * 64 ) +
( $values[2] % 64 );

} else if ($looking_for == 2) {

$number = ( ( $values[0] % 32 ) * 64 ) +
( $values[1] % 64 );

}

$unicode[] = $number;
$values = array();
Expand Down
16 changes: 16 additions & 0 deletions tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,22 @@ public function testUnicode()
$this->assertEquals($expected, $count);
}

public function testSurrogateUnicode()
{
$text = "🎈";

$count = SMSCounter::count($text);

$expected = new stdClass();
$expected->encoding = SMSCounter::UTF16;
$expected->length = 2;
$expected->per_message= 70;
$expected->remaining = 68;
$expected->messages = 1;

$this->assertEquals($expected, $count);
}

public function testRemoveNonGSMChars(){
$text = "no-unicode-remaining";

Expand Down

0 comments on commit fcd8546

Please sign in to comment.