如果HTML maxlength是110,为什么我可以输入114个字符?
<input type="text" id="title" name="title" size="50" maxlength="110" />
你看,maxlength是110,但是当我使用的时候发现我输入了114个字符,我很震惊
echo strlen($title);
Any idea?
顺便说一句,我输入的是日语文本。怎么了?
<input type="text" id="title" name="title" size="50" maxlength="110" />
You see, the maxlength is 110, but I was shocked when I found that I input 114 characters when I used
echo strlen($title);
Any idea?
By the way, what I input was Japanese text. What's wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
日语文本很可能使用多字节字符编码。因此,您应该使用 mb_strlen()。
最好的祝愿,
法比安
Japanese text is most probably using a multibyte character encoding. Therefore you should make use of mb_strlen().
Best wishes,
Fabian
PHP 的字符串处理基于 1 个字符 = 1 个字节的假设,并让程序员负责处理所有地方的编码,并记住在处理可能使用多字节编码的字符串时使用更新的 mb_* 函数。
PHP's string handling, which is based on the assumption that 1 character = 1 byte and leaves it to the programmer to tyke care of encodings everywhere and remember to use the more recent mb_* functions whenever dealing with strings that may use multibyte encodings.
maxlength 只是一个浏览器锁,但可以通过任何方式绕过它,包括通过 unicode 编码。
你最好为此提供一个服务器端控件
maxlength is just a browser lock but it's bypassable in any kind of way including by the unicode encoding.
You better provide a server side control for this