benmorel/gsm-charset-converter
Composer 安装命令:
composer require benmorel/gsm-charset-converter
包简介
Converts GSM 03.38 strings to and from UTF-8
关键字:
README 文档
README
A PHP library to convert GSM 03.38, the charset used for SMS messaging, to and from UTF-8.
This library is well tested. The character maps used have been cross-checked against multiple sources, and when in doubt, a test has been performed on a real SMS gateway.
The library offers optional transliteration: unsupported characters can be replaced with a close variant. For example, the ë character can be replaced with e.
Known limitations:
- Only the default alphabet and extension table are supported at the moment; this is the alphabet that must be supported by every device and network element according to the standard. Other alphabets exist but this project does not currently aim to support them.
- Transliteration may not be available for all characters that could have a close equivalent in the GSM charset; this library supports transliteration of LATIN1 chars and several other languages. If you feel like another UTF-8 character could be transliterated, please open an issue!
Installation
This library is installable via Composer:
composer require benmorel/gsm-charset-converter
Requirements
This library requires PHP >= 7.4, and the mbstring extension.
Project status & release process
This library is under development.
The current releases are numbered 0.x.y. When a non-breaking change is introduced (adding new methods, optimizing
existing code, etc.), y is incremented.
When a breaking change is introduced, a new 0.x version cycle is always started.
It is therefore safe to lock your project to a given release cycle, such as 0.3.*.
If you need to upgrade to a newer release cycle, check the release history
for a list of changes introduced by each further 0.x.0 version.
Usage
Converting GSM 03.38 strings to UTF-8
The convertGsmToUtf8() method takes one parameter:
use BenMorel\GsmCharsetConverter\Converter; $converter = new Converter(); $utf8 = $converter->convertGsmToUtf8('...');
The input string must be a valid GSM 03.38 string, or an InvalidArgumentException is thrown.
The input string is expected to be unpacked: 7-bit chars in 8-bit bytes with a leading zero bit, just like ASCII chars.
Converting UTF-8 strings to GSM 03.38
The convertUtf8ToGsm() method accepts 3 parameters:
- a valid UTF-8 input string;
- whether or not to attempt to transliterate incompatible chars;
- an optional string to replace unknown characters with.
If the input string is not valid UTF-8, an InvalidArgumentException is thrown.
The output is an unpacked GSM 03.38 string.
Without transliteration
$gsm = $converter->convertUtf8ToGsm('Helló', false, '?'); // Hell?
If the third parameter is not provided, and the string contains characters incompatible with GSM 03.38, an InvalidArgumentException is thrown.
With transliteration
$gsm = $converter->convertUtf8ToGsm('Helló', true, '?'); // Hello
If the third parameter is not provided, and the string contains characters incompatible with GSM 03.38 and not transliterable, an InvalidArgumentException is thrown.
Cleaning up UTF-8 strings to ensure that a message is sent in the GSM charset
Nowadays, most online SMS gateways accept UTF-8 as input; however, some of them do not provide a way to force a message to be sent in the GSM charset.
As a result, you may end up with extra charges caused by your SMS being sent in Unicode (UCS-2) format, causing the segmentation of messages in multiple parts, just because your SMS message contains an unforeseen accented character or emoji.
The library provides a method, cleanUpUtf8String(), that prevents these bad surprises, by returning a UTF-8 string that contains only characters that can be safely converted to the GSM charset.
This method accepts the same parameters as convertUtf8ToGsm():
$utf8 = $converter->cleanUpUtf8String('Helló', false, '?'); // Hell? $utf8 = $converter->cleanUpUtf8String('Helló', true, '?'); // Hello
Packing 7-bit strings into 8-bit binary strings
To fit 160 7-bit characters into a 140 bytes SMS, the characters have to be packed into a binary, 8-bit string.
The Packer class provides functionality to pack and unpack strings in this format:
use BenMorel\GsmCharsetConverter\Packer; $packer = new Packer(); $packed = $packer->pack('ABC'); // the binary string 41E110 $string = $packer->unpack("\x41\xE1\x10"); // ABC
Note that pack() throws an InvalidArgumentException if the input string contains 8-bit chars (i.e. chars with the leading bit set).
benmorel/gsm-charset-converter 适用场景与选型建议
benmorel/gsm-charset-converter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 484.98k 次下载、GitHub Stars 达 16, 最近一次更新时间为 2018 年 11 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「sms」 「gsm」 「GSM 03.38」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 benmorel/gsm-charset-converter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 benmorel/gsm-charset-converter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 benmorel/gsm-charset-converter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
SDK for payment gateway PlatbaMobilom.sk for PHP7.0
Extensible library for building notifications and sending them via different delivery channels.
MvcCore - Extension - Form - Field - Text - form field types - input:text, input:email, input:password, input:search, input:tel, input:url and textarea.
GlobalSportsMedia (GSM) API client
A Laravel package for sending SMS using the SMPP protocol, built on top of the PHP-SMPP library. This package provides a simple and efficient way to integrate SMS functionality into your Laravel applications.
yii2-sms expand
统计信息
- 总下载量: 484.98k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 16
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-11-22