PHP function substr() error

Stoikidis picture Stoikidis · Dec 29, 2009 · Viewed 20.9k times · Source

When I use substr() I get a strange character at the end

$articleText = substr($articleText,0,500);

I have an output of 500 chars and � <--

How can I fix this? Is it an encoding problem? My language is Greek.

Answer

Pascal MARTIN picture Pascal MARTIN · Dec 29, 2009

substr is counting using bytes, and not characters.

greek probably means you are using some multi-byte encoding, like UTF-8 -- and counting per bytes is not quite good for those.

Maybe using mb_substr could help, here : the mb_* functions have been created specifically for multi-byte encodings.