我的表中有一个类型为TINYTEXT
. 该字段的编码是UTF8-UNICODE-ci
。如果中国人在该字段中输入内容,则限制为 88 个字符,但如果伊朗人在波斯语中输入内容,则限制为 135 个字符,依此类推。所以最大文本长度根据 unicode 变化。如何克服这种情况并拥有基于特定语言的单词计数器?
有办法吗?我找不到它的方法;(
PS:我已经阅读了整个jQuery APIs,但在 PHP 中找不到类似 mb_strlen() 的东西。
我的表中有一个类型为TINYTEXT
. 该字段的编码是UTF8-UNICODE-ci
。如果中国人在该字段中输入内容,则限制为 88 个字符,但如果伊朗人在波斯语中输入内容,则限制为 135 个字符,依此类推。所以最大文本长度根据 unicode 变化。如何克服这种情况并拥有基于特定语言的单词计数器?
有办法吗?我找不到它的方法;(
PS:我已经阅读了整个jQuery APIs,但在 PHP 中找不到类似 mb_strlen() 的东西。
“字数计数器”我假设您的意思是“字符计数器”,因为您的问题是关于字符的。
构建这个计数器有两个部分:
需要一种方法来计算 UTF-8 字符串中的字节数。谢天谢地,其他人已经回答了这个问题:
encodeURIComponent(text).replace(/%[A-F\d]{2}/g, 'U').length
每次用户键入内容时都需要一种方法来触发计数功能。我们可以使用keyup
事件:
$('textarea').keyup(function () { ... });
这是一个完整的示例:http: //jsfiddle.net/jefferyto/DWwQr/
更新:我猜你正在寻找的是一个倒计时的计数器,指示用户可以输入多少个字符。
从技术上讲,这并不难计算,如果您假设 1 个字符中有多少字节:
(characters left) = Math.floor((255 - (num bytes in string)) / (num bytes in character))
但从用户的角度来看,这不是一个好主意:
你会用作num bytes in character
什么?
如果您使用 1,则计数器一开始会显示 255,但这仅适用于 ASCII 字符;用户将无法输入 255 个汉字。
您选择的任何数字对于您的部分用户来说都是不正确的。
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.
您可以从 mysql 获取 UTF-8 并在计数之前将其转换为 javascript 中的 UTF-16。我似乎记得我曾经做过这样一个转换的旧项目。
编辑:我们使用的代码似乎起源于这里。
可能这会有所帮助http://tympanus.net/codrops/2009/11/08/jmaxinput-twitter-like-textarea/ 我用这个 س 检查了它,它把它当作一个字符。