javascript - international Count sms characters -


I have found, but it does not support international characters like Chinese, Japanese, Thai, etc.

  var $ Balance = $ ('# left), $ message = $ remaining.ext (); $ ('#messages'). Key (function () {var character = this.value.length, message = Math.Cecil (character / 160), balance = message * 160 - (character% (message * 160) || message * 160); rest Text (Balance + 'Characters Remaining'); $ messages.text (Message + 'Message');});   

Here are some examples of incorrect character counting:

æ ?? Åå ¥ ½ï¼ ?? È «???? One ???? One half a £ E ¯äº ?? Å ???? I ¼ ?? & Lt; & Lt; 11 characters

Reply thread one £ one · One? & Lt; & Lt; 17 letters

a word ???? One ?? One ?? One ?? One ???? One ?? Line Line ?? E £ ?? A ?? ¹ã ?? |  ???? A ?? ¾a ???? à ???? I ¼ ?? & Lt; & Lt; 18 letters

"????" Line Line Line Line ????, '? '?? Answer: Thread? <18>

оÐ

You can not really believe in" characters "here, according to the article on Wikipedia, one of three different encodings is used (7 bit GSM, 8 bit GSM And uk F-16). So first you should know what encoding you will use

If you know that you will always use UTF-16, you can calculate that the number of a string There will be 70 16-bit code units in a standard SMS, but it will limit messages to 70 in Latin characters. So if you have 160 characters (with 7-bit encoding) or 140 for Latin characters Letter (8-bit encoding With), then you have to distinguish between three cases.

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

  var message = "æ ?? åå ¥ ½ï¼? È« ?? a ?? ?? a half a ???? ¡£ ¯äº ???? Å ???? ¼ ?? "; Var utf16codeUnits = 0; (Var i = 0, len = message.length; i & lt; len; i ++) {utf16codeUnits + = message.charCodeAt (i) & lt; 0x10000? 1: 2; }   

BTW, it will be come with the same number that you posted as "wrong", so you have to explain why you are wrong Believe it. / P>


Edit

Even though I have already accepted it, I have hacked a function quickly (as far as I can say) GSM 7-bit (if possible) and UTF-16 size of SMS message:

Comments