I see a lot of usage in the field where people rely on this function to truncate a string to a given length and append some chars at the end following example #1 in the above documentation.
While this works just fine with Western alphabets, it should be noted that a string's width is NOT necessarily the same as its length.
In Chinese, Japanese and Korean, some characters can be represented as full or half width, which may lead to unexpected results...
<?php
$str = ['English' => 'Switzerland',
'Half width' => 'スイス',
'Full width' => 'スイス',
];
foreach ($str as $w => $s) {
printf("%-10s: %s (bytes=%d chars=%d width=%d)\nSubstring : %s\nTrim width: %s\n\n",
$w, $s,
strlen($s), mb_strlen($s), mb_strwidth($s),
mb_substr($s, 0, 3),
mb_strimwidth($s, 0, 3)
);
}
>?