国际计数短信字符 [英] international Count sms characters

查看:177
本文介绍了国际计数短信字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现使用jQuery计数字符/短信,但它不支持国际化中文,日文,泰文等字符.

I found Count characters/sms using jQuery, but it does not support international characters such as Chinese, Japanese, Thai, etc.

var $remaining = $('#remaining'),
    $messages = $remaining.next();

$('#message').keyup(function(){
    var chars = this.value.length,
        messages = Math.ceil(chars / 160),
        remaining = messages * 160 - (chars % (messages * 160) || messages * 160);

    $remaining.text(remaining + ' characters remaining');
    $messages.text(messages + ' message(s)');
});

以下是一些错误的字符计数示例:

Here are some examples of incorrect character counts:

您好,请问你吃饭了吗?<< 11个字符

您好,請問你吃飯了嗎? << 11 characters

สวัสดีคุณกินหรือ? << 17个字符

สวัสดีคุณกินหรือ? << 17 characters

こんにちは,あなたは食べていますか?<< 18个字符

こんにちは、あなたは食べていますか? << 18 characters

안녕하세요,당신이먹는? << 17个字符

안녕하세요, 당신이 먹는 거죠? << 17 characters

हैलो,आप? << 18个字符

हैलो, आप खाते हैं? << 18 characters

Добрыдзень,выясьце? << 22个字符

Добры дзень, вы ясьце? << 22 characters

如何使用非ASCII字符进行这项工作?

How can I make this work with non-ASCII characters?

推荐答案

您在这里不能真正算作字符".根据Wikipedia上的 SMS 文章,SMS使用了三种不同的编码之一(7位GSM,8位GSM和UTF-16).因此,首先您需要知道/确定要使用的编码.

You can't really count in "characters" here. According to the SMS article on Wikipedia one of three different encodings are used for SMS (7-bit GSM, 8-bit GSM and UTF-16). So first you'll need to know/decide which encoding you'll be using.

如果您知道将一直使用UTF-16,则可以计算 16位的数量代码单元将占用一个字符串.一个标准的SMS可以包含70个16位代码单元.但这也将拉丁字符中的消息限制为70个.因此,如果要对拉丁字符使用完整的160个字符(具有7位编码)或140个字符(具有8位编码),则需要区分这三种情况.

If you know you'll always be using UTF-16, then you can count the number of 16-bit code units a string will take up. A standard SMS can consist of 70 16-bit code units. But this will limit messages in Latin characters to 70, too. So if you want to use the full 160 characters (with 7-bit encoding) or 140 characters (with 8-bit encoding) for Latin characters, then you'll need to distinguish between the three cases.

计算UTF-16 16位代码单位的示例:

Example for counting UTF-16 16-bit code units:

var message = "您好,請問你吃飯了嗎?";

var utf16codeUnits = 0;

for (var i = 0, len = message.length; i < len; i++) {
  utf16codeUnits += message.charCodeAt(i) < 0x10000 ? 1 : 2;
}

顺便说一句,此拿出与您发布为不正确"的数字相同的数字,因此您需要解释为什么您认为它们不正确.

BTW, this will come up with then same numbers you posted as "incorrect", so you'll need to explain why you consider them incorrect.

编辑

尽管已被接受,但我还是迅速破解了一个函数,该函数可以正确地(据我所知)计算SMS消息的GSM 7位(如果可能)和UTF-16大小:http://jsfiddle.net/puKJb/

Despite being accepted already I quickly hacked up a function that correctly (as far as I can say) calculates the GSM 7-bit (if possible) and UTF-16 sizes of a SMS message: http://jsfiddle.net/puKJb/

这篇关于国际计数短信字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆