我想检查 PHP 上是否是字符串类型多字节。
知道如何实现这一点吗?
Example:
<?php!
$string = "I dont have idea that is what i am...";
if( is_multibyte( $string ) )
{
echo 'yes!!';
}else{
echo 'ups!';
}
?>
也许(规则8字节):
<?php
if( mb_strlen( $string ) > strlen() )
{
return true;
}
else
{
return false;
}
?>
I read:
可变宽度编码 - WIKI and
UTF-8 - 维基百科
有两种解释。首先,每个字符都是多字节的。第二是字符串至少包含一个多字节字符。如果您对处理无效字节序列感兴趣,请参阅https://stackoverflow.com/a/13695364/531320了解详情。
function is_all_multibyte($string)
{
// check if the string doesn't contain invalid byte sequence
if (mb_check_encoding($string, 'UTF-8') === false) return false;
$length = mb_strlen($string, 'UTF-8');
for ($i = 0; $i < $length; $i += 1) {
$char = mb_substr($string, $i, 1, 'UTF-8');
// check if the string doesn't contain single character
if (mb_check_encoding($char, 'ASCII')) {
return false;
}
}
return true;
}
function contains_any_multibyte($string)
{
return !mb_check_encoding($string, 'ASCII') && mb_check_encoding($string, 'UTF-8');
}
$data = ['東京', 'Tokyo', '東京(Tokyo)'];
var_dump(
[true, false, false] ===
array_map(function($v) {
return is_all_multibyte($v);
},
$data),
[true, false, true] ===
array_map(function($v) {
return contains_any_multibyte($v);
},
$data)
);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)