Unicode-safe strings with mb_*
strlen() counts bytes. For Urdu, Arabic or emoji use the mb_ functions.
PHP · Unicode-safe strings with mb_*Intermediate
$text = 'پاکستان زندہ باد';
echo strlen($text); // bytes (not characters)
echo mb_strlen($text); // 16 characters
echo mb_substr($text, 0, 7); // 'پاکستان'
echo mb_strtoupper('straße'); // 'STRASSE'
function excerpt(string $s, int $max = 20): string
{
return mb_strlen($s) > $max ? mb_substr($s, 0, $max - 1) . '…' : $s;
}
echo excerpt($text, 8); // 'پاکستان…' is 8 charactersmb_stringunicodeutf8