Javascript字符串长度与PHP mb_strlen不同 [英] Javascript String Length Differs From PHP mb_strlen
问题描述
我使用 document.getElementById(text)。value.length
通过javascript获取字符串长度,并使用 mb_strlen($ _ POST [ 'text'])
通过PHP获取字符串长度,两者差别很大。在获取字符串长度之前,回车符是在javascript中转换的,但我猜有些字符不计算在内。
I use document.getElementById("text").value.length
to get the string length through javascript, and mb_strlen($_POST['text'])
to get the string length by PHP and both differs very much. Carriage returns are converted in javascript before getting the string length, but I guess some characters are not being counted.
例如,
[b] 15。 Umieszczanie obrazka z logo nastroniezespołu[/ b]
[b]15. Umieszczanie obrazka z logo na stronie zespołu[/b]
这个文本块在javascript中计算57,在PHP中计算58。当文本变长时,差异会增加。有什么办法可以解决这个问题吗?
This block of text is calculated 57 in javascript and 58 in PHP. When the text gets long, the difference increases. Is there any way to overcome this?
推荐答案
如果你想要获取UTF-8编码字符串的长度PHP,您应该在 mb_strlen
的第二个参数中指定编码,如下所示:
If you're trying to get the length of an UTF-8 encoded string in PHP, you should specify the encoding in the second parameter of mb_strlen
, like so:
mb_strlen($_POST['text'], 'UTF-8')
,别忘了在POST-var上调用 stripslashes
。
Also, don't forget to call stripslashes
on the POST-var.
这篇关于Javascript字符串长度与PHP mb_strlen不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!