Weird characters appear after I use php's mb_substr() on a string
-
25-02-2021 - |
Question
I'm developing a web site with PHP (5.3.5, Ubuntu) and all the content is in Spanish. I would like to cut the text when it doesn't fit the space designated for it. I have the following meta tag in the php file where I want to do this: <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />.
The text comes from a MySQL database where charset is latin1 and collation latin1_spanish_ci. I'm trying to cut the text with the mb_substr() function. But it isn't working correctly. For example, let's say I want to cut Short Psicodélico to Short Psicodéli, the function would be:
mb_substr('Short Psicodélico', 0, 15, 'ISO-8859-1');
But the result is something like this: Short Psicod&ea. The e with the diacritic is transformed in &ea and I don't know why. I think it has something to do with the character encoding but I don't know exactly how. If I don't use this function the characters appear as they should, instead of Short Psicod&ea it shows Short Psicodélico.
Solution
The text is encoded in the database as "Short Psicodélico"
. You will need to scrub your database to remove the encoding, as well as fix your input routines to make certain that text is not saved to the database encoded.