0

I have a field in my table with the type of TINYTEXT. the encoding of the field is UTF8-UNICODE-ci. If a chinese type something in the field the limit would be 88 characters, but if an iranian type something in persian there will about 135 characters and so on. So max text length is varied based on unicode. How to overcome this situation and have a word counter based on the specific language?
Is there a way for it? I couldn't find an approach for it ;(

P.S.: I've read the whole jQuery APIs, but couldn't find something like mb_strlen() in PHP.

2
  • if you switch to UTF-16 you got 128 chars in all language, otherwise you can count bout chars and bytes of the text (i guess jquery can do that, like phps strlen and mb_strlen), and calculate the average byte/char and then calculate byts left / (averege bytes/char)
    – Puggan Se
    Jun 30, 2012 at 9:19
  • The problem is that UTF-16 is only available in MySQL 5.5 and 5.6
    – Alireza
    Jun 30, 2012 at 11:17

3 Answers 3

1
+50

By "word counter" I assume you mean "character counter" since your question talks about characters.

There are two parts to building this counter:

  1. Need a way to count the number of bytes in a UTF-8 string. Thankfully, someone else already answered this question:

    encodeURIComponent(text).replace(/%[A-F\d]{2}/g, 'U').length
    
  2. Need a way to trigger the count function every time the user types something. We can use the keyup event:

    $('textarea').keyup(function () { ... });
    

Here is a completed example: http://jsfiddle.net/jefferyto/DWwQr/


Update: I guess what you're looking for is a counter that counts down, indicating how many characters left that the user can enter.

Technically this would not be hard to compute, if you make an assumption on how many bytes go into 1 character:

(characters left) = Math.floor((255 - (num bytes in string)) / (num bytes in character))

But this would not be a good idea from a user perspective:

  1. What would you use as num bytes in character?

    If you use 1, then at the beginning the counter would say 255, but that's only true for ASCII characters; the user wouldn't be able to enter 255 Chinese characters.

    Any number you choose would not be correct for a portion of your users.

  2. When the user starts entering text, the counter will not count down 1 by 1, as the user would expect, but rather in incomprehensible steps (incomprehensible to the user).

    Again assuming 1 byte per character for the calculation, before the user has entered any text, the counter will say 255. If the user enters a 4-byte character, the counter would change to 251.

    It makes no sense to the user that they entered 1 character but the counter decreased by some other number.

I suggest using VARCHAR instead of TINYTEXT; the length of a VARCHAR field is defined with a number of characters instead of bytes. Doing so means your character count can be stable and correct.

3
  • Sorry I meant character counter. when I type س in the textarea the counter shows 2! It's actually one letter not two.
    – Alireza
    Jul 5, 2012 at 12:22
  • @phpGeek Did you want a "number of characters" count or a "number of bytes needed to store" count? س is 1 character but requires 2 bytes to store (in UTF-8 encoding). If you wanted a "number of characters" count, you can just use string.length.
    – Jeffery To
    Jul 5, 2012 at 12:38
  • I bestow the bounty to you, thank you for your update. The answer was behind changing tinytext to varchar otherwise there wasn't any way to complete the task correctly.
    – Alireza
    Jul 7, 2012 at 17:37
0

You could get the UTF-8 from mysql and convert it to UTF-16 in javascript before counting. I seem to remember an old project I worked on performing such a conversion.

EDIT: The code we used seems to have originated here.

0

may be this will help http://tympanus.net/codrops/2009/11/08/jmaxinput-twitter-like-textarea/ i checked it with this س it counts this as a single character.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.