Important, if you're looking to trim/cut/truncate a string so that it will fit a certain byte size (for example to fit in a database field), look at: mb_strcut()
(PHP 4 >= 4.0.6, PHP 5, PHP 7)
mb_strwidth — 返回字符串的宽度
$str
[, string $encoding
= mb_internal_encoding()
] )
返回 string 类型 str
的宽度。
多字节字符通常是单字节字符的两倍宽度。
字符 | 宽度 |
---|---|
U+0000 - U+0019 | 0 |
U+0020 - U+1FFF | 1 |
U+2000 - U+FF60 | 2 |
U+FF61 - U+FF9F | 1 |
U+FFA0 - | 2 |
string str
的宽度。
Important, if you're looking to trim/cut/truncate a string so that it will fit a certain byte size (for example to fit in a database field), look at: mb_strcut()
to convert a multi-byte character into hex strings:
<?php
$b = "現,市民派利市的習慣亦有所改變";
printf("length of string: %d \n", mb_strlen($b, 'UTF-8'));
for ($i=0; $i < mb_strlen($b, 'UTF-8'); $i++){
$ch = mb_substr($b, $i, 1, 'UTF-8');
$chlen = strlen($ch);
$hexs = '';
for ($j=0; $j < $chlen; $j++)
$hexs = $hexs . sprintf("%x", ord($ch[$j]));
printf ("width=%d => '%s' |hex=%s\n", $chlen, $ch, $hexs );
}
?>
width=3 => '現' |hex=e78fbe
width=3 => ',' |hex=efbc8c
width=3 => '市' |hex=e5b882
width=3 => '民' |hex=e6b091
width=3 => '派' |hex=e6b4be
width=3 => '利' |hex=e588a9
width=3 => '市' |hex=e5b882
width=3 => '的' |hex=e79a84
width=3 => '習' |hex=e7bf92
width=3 => '慣' |hex=e685a3
width=3 => '亦' |hex=e4baa6
width=3 => '有' |hex=e69c89
width=3 => '所' |hex=e68980
width=3 => '改' |hex=e694b9
width=3 => '變' |hex=e8ae8a
to convert a multi-byte character into hex strings:
$b = "現,市民派利市的習慣亦有所改變";
printf("length of string: %d <br>", mb_strlen($b, 'UTF-8'));
for ($i=0; $i < mb_strlen($b, 'UTF-8'); $i++){
$ch = mb_substr($b, $i, 1, 'UTF-8');
$chlen = mb_strwidth($ch, 'UTF-8');
$hexs = '';
for ($j=0; $j < $chlen; $j++)
$hexs = $hexs . sprintf("%x", ord($ch[$j]));
printf ("width=%d => '%s' |hex=%s<br>", $chlen, $ch, $hexs );
}
Note: mb_strwidth is NOT returning bytes. It's returning the width of monotype characters. (In some languages, some characters will take up 2 character widths if displayed in monotype.)