php mb_strlen() 够了吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3640862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mb_strlen() is it enough?
提问by PHPLOVER
When counting the length of an UTF-8 string in PHP I use mb_strlen()
.
在计算 PHP 中 UTF-8 字符串的长度时,我使用mb_strlen()
.
For example:
例如:
if (mb_strlen($name, 'UTF-8') < 3) {
$error .= 'Name is required. Minimum of 3 characters required in name.';
}
As the text fields can accept any language (multilanguage), I want to make sure that PHP will count mutltilanguage UTF-8 characters correctly.
由于文本字段可以接受任何语言(多语言),我想确保 PHP 将正确计算多语言 UTF-8 字符。
Is this sufficient or do I need to use other methods? I am concerned PHP may get it wrong for some reason, or maybe I am just being skeptical, but I don't want people bypassing important validation because PHP is getting it wrong.
这是足够的还是我需要使用其他方法?我担心 PHP 可能由于某种原因出错,或者我只是持怀疑态度,但我不希望人们因为 PHP 出错而绕过重要的验证。
回答by Pramendra Gupta
Correct
正确的
if (mb_strlen($name, 'UTF-8') < 3) is sufficient enough
make sure header is correct
确保标题正确
HTTP-header (Content-Type: text/html; charset=UTF-8)
you can also check alternative for some reason
您也可以出于某种原因检查替代方案
strlen(utf8_decode($string))
回答by Nasser Al-Wohaibi
In addition to JapanPro'sanswer:
Your HTML can have :
除了JapanPro 的回答:
您的 HTML 可以具有:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
instead of an implicit header declaration:
而不是隐式标头声明:
HTTP-header (Content-Type: text/html; charset=UTF-8)