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));

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.4.130.0110.01018.00
8.4.120.0070.00220.60
8.4.110.0130.00820.49
8.4.100.0100.01218.61
8.4.90.0130.00622.40
8.4.80.0050.00518.73
8.4.70.0050.00318.94
8.4.60.0100.01118.85
8.4.50.0140.00818.41
8.4.40.0100.01019.64
8.4.30.0070.00418.76
8.4.20.0100.00018.09
8.4.10.0130.00318.75
8.3.250.0090.01019.11
8.3.240.0090.01116.81
8.3.230.0130.00616.84
8.3.220.0060.00219.14
8.3.210.0080.00916.90
8.3.200.0030.00516.77
8.3.190.0120.00617.19
8.3.180.0080.00319.02
8.3.170.0060.01218.39
8.3.160.0090.00916.74
8.3.150.0080.00017.04
8.3.140.0040.00417.00
8.3.130.0050.00318.55
8.3.120.0030.00620.63
8.3.110.0040.00417.05
8.3.100.0090.00016.90
8.3.90.0030.00626.77
8.3.80.0000.01216.63
8.3.70.0120.00317.00
8.3.60.0090.00917.00
8.3.50.0060.01520.28
8.3.40.0040.01120.32
8.3.30.0110.00319.21
8.3.20.0050.00324.18
8.3.10.0000.00824.66
8.3.00.0040.00426.16
8.2.290.0110.00520.25
8.2.280.0100.00918.80
8.2.270.0110.00716.77
8.2.260.0060.00316.89
8.2.250.0150.00417.17
8.2.240.0050.00517.00
8.2.230.0090.00020.94
8.2.220.0070.00324.06
8.2.210.0080.00026.77
8.2.200.0060.00316.75
8.2.190.0070.01416.63
8.2.180.0180.00025.92
8.2.170.0070.01019.16
8.2.160.0130.00322.96
8.2.150.0000.00825.66
8.2.140.0020.00524.66
8.2.130.0040.00426.16
8.2.120.0040.00419.95
8.2.110.0080.00022.11
8.2.100.0040.00918.04
8.2.90.0000.00819.26
8.2.80.0000.00817.97
8.2.70.0050.00317.63
8.2.60.0040.00417.93
8.2.50.0040.00418.07
8.2.40.0050.00318.22
8.2.30.0030.00518.40
8.2.20.0020.00520.50
8.2.10.0040.00418.19
8.2.00.0000.00719.36
8.1.330.0090.01118.15
8.1.320.0130.00516.32
8.1.310.0040.00416.85
8.1.300.0090.00020.16
8.1.290.0000.01030.84
8.1.280.0110.00425.92
8.1.270.0000.00923.99
8.1.260.0080.00026.35
8.1.250.0030.00628.09
8.1.240.0060.00322.48
8.1.230.0000.01219.12
8.1.220.0030.00617.74
8.1.210.0080.00018.77
8.1.200.0000.00917.38
8.1.190.0000.00817.48
8.1.180.0000.00818.10
8.1.170.0060.00318.76
8.1.160.0040.00418.82
8.1.150.0080.00018.78
8.1.140.0040.00717.51
8.1.130.0070.00018.77
8.1.120.0000.00717.58
8.1.110.0040.00417.59
8.1.100.0040.00417.61
8.1.90.0040.00417.48
8.1.80.0000.00817.54
8.1.70.0000.00817.49
8.1.60.0000.00917.73
8.1.50.0040.00417.73
8.1.40.0070.00717.67
8.1.30.0080.00417.85
8.1.20.0180.00317.83
8.1.10.0060.00617.64
8.1.00.0250.00017.77
8.0.300.0000.00718.77
8.0.290.0040.00417.00
8.0.280.0000.00818.52
8.0.270.0000.00717.05
8.0.260.0030.00318.54
8.0.250.0040.00417.15
8.0.240.0070.00017.04
8.0.230.0070.00017.08
8.0.220.0030.00316.93
8.0.210.0050.00317.10
8.0.200.0050.00517.13
8.0.190.0000.00817.12
8.0.180.0000.00717.07
8.0.170.0070.00716.85
8.0.160.0120.00616.81
8.0.150.0150.00616.95
8.0.140.0170.00316.93
8.0.130.0190.00016.89
8.0.120.0130.00716.85
8.0.110.0160.00316.99
8.0.100.0140.00517.02
8.0.90.0240.00016.95
8.0.80.0240.00017.04
8.0.70.0120.00817.01
8.0.60.0090.00916.98
8.0.50.0180.00317.02
8.0.30.0140.00716.95
8.0.20.0120.00817.00
8.0.10.0140.00717.18
7.4.330.0000.00515.55
7.4.320.0030.00316.87
7.4.300.0040.00416.72
7.4.290.0030.00316.61
7.4.280.0070.00416.74
7.4.270.0140.01016.73
7.4.260.0200.00316.78
7.4.250.0250.00016.64
7.4.240.0200.00716.86
7.4.230.0150.00616.84
7.4.220.0190.00016.58
7.4.210.0150.00316.76
7.4.200.0190.00016.75
7.4.190.0200.00016.89
7.4.180.0140.00716.77
7.4.160.0100.01016.74
7.4.150.0150.00416.71
7.4.140.0150.00416.68
7.4.130.0120.00616.85
7.4.120.0090.00916.81
7.4.110.0060.01116.84
7.4.100.0140.00616.84
7.4.90.0160.00316.68
7.4.80.0110.00616.78
7.4.70.0190.00016.83
7.4.60.0160.00316.61
7.4.50.0130.00816.65
7.4.40.0030.00616.79
7.4.30.0090.00616.86
7.4.20.0140.00416.68
7.4.10.0050.00516.72
7.4.00.0110.00016.76

preferences:
36.52 ms | 403 KiB | 5 Q