3v4l.org

run code in 300+ PHP versions simultaneously
<?php // https://github.com/onlinecity/php-smpp/blob/master/gsmencoder.class.php class GsmEncoder { /** * Encode an UTF-8 string into GSM 03.38 * Since UTF-8 is largely ASCII compatible, and GSM 03.38 is somewhat compatible, unnecessary conversions are removed. * Specials chars such as € can be encoded by using an escape char \x1B in front of a backwards compatible (similar) char. * UTF-8 chars which doesn't have a GSM 03.38 equivalent is replaced with a question mark. * UTF-8 continuation bytes (\x08-\xBF) are replaced when encountered in their valid places, but * any continuation bytes outside of a valid UTF-8 sequence is not processed. * * @param string $string * @return string */ public static function utf8_to_gsm0338($string) { $dict = array( '@' => "\x00", '£' => "\x01", '$' => "\x02", '¥' => "\x03", 'è' => "\x04", 'é' => "\x05", 'ù' => "\x06", 'ì' => "\x07", 'ò' => "\x08", 'Ç' => "\x09", 'Ø' => "\x0B", 'ø' => "\x0C", 'Å' => "\x0E", 'å' => "\x0F", 'Δ' => "\x10", '_' => "\x11", 'Φ' => "\x12", 'Γ' => "\x13", 'Λ' => "\x14", 'Ω' => "\x15", 'Π' => "\x16", 'Ψ' => "\x17", 'Σ' => "\x18", 'Θ' => "\x19", 'Ξ' => "\x1A", 'Æ' => "\x1C", 'æ' => "\x1D", 'ß' => "\x1E", 'É' => "\x1F", // all \x2? removed // all \x3? removed // all \x4? removed 'Ä' => "\x5B", 'Ö' => "\x5C", 'Ñ' => "\x5D", 'Ü' => "\x5E", '§' => "\x5F", '¿' => "\x60", 'ä' => "\x7B", 'ö' => "\x7C", 'ñ' => "\x7D", 'ü' => "\x7E", 'à' => "\x7F", '^' => "\x1B\x14", '{' => "\x1B\x28", '}' => "\x1B\x29", '\\' => "\x1B\x2F", '[' => "\x1B\x3C", '~' => "\x1B\x3D", ']' => "\x1B\x3E", '|' => "\x1B\x40", '€' => "\x1B\x65" ); $converted = strtr($string, $dict); // Replace unconverted UTF-8 chars from codepages U+0080-U+07FF, U+0080-U+FFFF and U+010000-U+10FFFF with a single ? return preg_replace('/([\\xC0-\\xDF].)|([\\xE0-\\xEF]..)|([\\xF0-\\xFF]...)/m','?',$converted); } /** * Count the number of GSM 03.38 chars a conversion would contain. * It's about 3 times faster to count than convert and do strlen() if conversion is not required. * * @param string $utf8String * @return integer */ public static function countGsm0338Length($utf8String) { $len = mb_strlen($utf8String,'utf-8'); $len += preg_match_all('/[\\^{}\\\~€|\\[\\]]/mu',$utf8String,$m); return $len; } /** * Pack an 8-bit string into 7-bit GSM format * Returns the packed string in binary format * * @param string $data * @return string */ public static function pack7bit($data) { $l = strlen($data); $currentByte = 0; $offset = 0; $packed = ''; for ($i = 0; $i < $l; $i++) { // cap off any excess bytes $septet = ord($data[$i]) & 0x7f; // append the septet and then cap off excess bytes $currentByte |= ($septet << $offset) & 0xff; // update offset $offset += 7; if ($offset > 7) { // the current byte is full, add it to the encoded data. $packed .= chr($currentByte); // shift left and append the left shifted septet to the current byte $currentByte = $septet = $septet >> (7 - ($offset - 8 )); // update offset $offset -= 8; // 7 - (7 - ($offset - 8)) } } if ($currentByte > 0) $packed .= chr($currentByte); // append the last byte return $packed; } } $resultat = GsmEncoder::utf8_to_gsm0338("abc é œ ♥"); var_export($resultat); echo "\n"; var_export(unpack("H*", $resultat));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9h3EK
function name:  (null)
number of ops:  16
compiled vars:  !0 = $resultat
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   88     0  E >   INIT_STATIC_METHOD_CALL                                  'GsmEncoder', 'utf8_to_gsm0338'
          1        SEND_VAL                                                 'abc+%C3%A9+%C5%93+%E2%99%A5'
          2        DO_FCALL                                      0  $1      
          3        ASSIGN                                                   !0, $1
   89     4        INIT_FCALL                                               'var_export'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                                 
   90     7        ECHO                                                     '%0A'
   91     8        INIT_FCALL                                               'var_export'
          9        INIT_FCALL                                               'unpack'
         10        SEND_VAL                                                 'H%2A'
         11        SEND_VAR                                                 !0
         12        DO_ICALL                                         $4      
         13        SEND_VAR                                                 $4
         14        DO_ICALL                                                 
         15      > RETURN                                                   1

Class GsmEncoder:
Function utf8_to_gsm0338:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9h3EK
function name:  utf8_to_gsm0338
number of ops:  14
compiled vars:  !0 = $string, !1 = $dict, !2 = $converted
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
   21     1        ASSIGN                                                   !1, <array>
   32     2        INIT_FCALL                                               'strtr'
          3        SEND_VAR                                                 !0
          4        SEND_VAR                                                 !1
          5        DO_ICALL                                         $4      
          6        ASSIGN                                                   !2, $4
   35     7        INIT_FCALL                                               'preg_replace'
          8        SEND_VAL                                                 '%2F%28%5B%5CxC0-%5CxDF%5D.%29%7C%28%5B%5CxE0-%5CxEF%5D..%29%7C%28%5B%5CxF0-%5CxFF%5D...%29%2Fm'
          9        SEND_VAL                                                 '%3F'
         10        SEND_VAR                                                 !2
         11        DO_ICALL                                         $6      
         12      > RETURN                                                   $6
   36    13*     > RETURN                                                   null

End of function utf8_to_gsm0338

Function countgsm0338length:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9h3EK
function name:  countGsm0338Length
number of ops:  14
compiled vars:  !0 = $utf8String, !1 = $len, !2 = $m
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
   47     1        INIT_FCALL                                               'mb_strlen'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 'utf-8'
          4        DO_ICALL                                         $3      
          5        ASSIGN                                                   !1, $3
   48     6        INIT_FCALL                                               'preg_match_all'
          7        SEND_VAL                                                 '%2F%5B%5C%5E%7B%7D%5C%5C%7E%E2%82%AC%7C%5C%5B%5C%5D%5D%2Fmu'
          8        SEND_VAR                                                 !0
          9        SEND_REF                                                 !2
         10        DO_ICALL                                         $5      
         11        ASSIGN_OP                                     1          !1, $5
   49    12      > RETURN                                                   !1
   50    13*     > RETURN                                                   null

End of function countgsm0338length

Function pack7bit:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
2 jumps found. (Code = 44) Position 1 = 33, Position 2 = 8
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 39
Branch analysis from position: 35
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 39
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 30
Branch analysis from position: 20
2 jumps found. (Code = 44) Position 1 = 33, Position 2 = 8
Branch analysis from position: 33
Branch analysis from position: 8
Branch analysis from position: 30
filename:       /in/9h3EK
function name:  pack7bit
number of ops:  41
compiled vars:  !0 = $data, !1 = $l, !2 = $currentByte, !3 = $offset, !4 = $packed, !5 = $i, !6 = $septet
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   RECV                                             !0      
   61     1        STRLEN                                           ~7      !0
          2        ASSIGN                                                   !1, ~7
   62     3        ASSIGN                                                   !2, 0
   63     4        ASSIGN                                                   !3, 0
   64     5        ASSIGN                                                   !4, ''
   65     6        ASSIGN                                                   !5, 0
          7      > JMP                                                      ->31
   67     8    >   INIT_FCALL                                               'ord'
          9        FETCH_DIM_R                                      ~13     !0, !5
         10        SEND_VAL                                                 ~13
         11        DO_ICALL                                         $14     
         12        BW_AND                                           ~15     $14, 127
         13        ASSIGN                                                   !6, ~15
   69    14        SL                                               ~17     !6, !3
         15        BW_AND                                           ~18     ~17, 255
         16        ASSIGN_OP                                     9          !2, ~18
   71    17        ASSIGN_OP                                     1          !3, 7
   73    18        IS_SMALLER                                               7, !3
         19      > JMPZ                                                     ~21, ->30
   75    20    >   INIT_FCALL                                               'chr'
         21        SEND_VAR                                                 !2
         22        DO_ICALL                                         $22     
         23        ASSIGN_OP                                     8          !4, $22
   77    24        SUB                                              ~24     !3, 8
         25        SUB                                              ~25     7, ~24
         26        SR                                               ~26     !6, ~25
         27        ASSIGN                                           ~27     !6, ~26
         28        ASSIGN                                                   !2, ~27
   79    29        ASSIGN_OP                                     2          !3, 8
   65    30    >   PRE_INC                                                  !5
         31    >   IS_SMALLER                                               !5, !1
         32      > JMPNZ                                                    ~31, ->8
   82    33    >   IS_SMALLER                                               0, !2
         34      > JMPZ                                                     ~32, ->39
         35    >   INIT_FCALL                                               'chr'
         36        SEND_VAR                                                 !2
         37        DO_ICALL                                         $33     
         38        ASSIGN_OP                                     8          !4, $33
   84    39    > > RETURN                                                   !4
   85    40*     > RETURN                                                   null

End of function pack7bit

End of class GsmEncoder.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
207.99 ms | 1016 KiB | 21 Q