Logic: I am getting username from DB and if it is greater than 30 in length then i show 30 characters with "..." appended at the end. Code is
$username = htmlspecialchars($username);
if(mb_strlen($username, 'utf-8')>30){
$username_trimmed = mb_substr($username, 0, 30, 'utf-8').'...';
}
and in my navivation I am just printing this username
<class="userName">Hello, <?php echo $username_trimmed; ?>
My encoding in set as utf-8
, and mbstring
extension is enabled in php.
Output of above code : It still breaks the accent character É
because it is multi-byte character and it is getting cut the in the middle.
Actual word is MARCHÉS
and output is:
Question what am I missing? mb_substr
should not consider it as a single character and should not stop it from breaking in the middle as it does?