Giang Nguyen
Giang Nguyen

Reputation: 485

displaying funny characters after substr() text with accent marks

I am using substr() function to cut strings. It worked ok with normal string but in case of string with accent marks, it displayed funny characters just before the cut like this:

Here is the code:

$rawtitle ="Tin đăng của giangvy1011 tin của dtdd";
$title = substr($rawtitle,0,36).'...';
echo $title;

Here is the result i got for the echo:

enter image description here

Is there any way around this or different function to archieve this? Many thanks

Upvotes: 3

Views: 101

Answers (1)

Federkun
Federkun

Reputation: 36924

substr is not multibyte safe. The character is represented by more than a byte. With substr you cut these byte sequences and you corrupt the string.

You need to use mb_substr instead.

Upvotes: 6

Related Questions