Getting that strange “black diamond” and other weird symbol characters that return text from a database?

It’s inevitable, right when you thought your website was going smoothly, suddenly those strange symbols like the “black diamond question marks” start popping up. I sometimes have to clean variables returned from form input and other external text with PHP’s “iconv” function, but if the data is coming from a database and showing up funky, try forcing PHP to read the data as “UTF-8”. What’s “UTF-8”, you ask? It’s an encoding method like “latin1” that we humans devised to make sure nothing will be easy. Yes, it’s just not complicated enough all those bits and bytes of programmed code to work together like a million cells in the human body in order to function and not collapse into a big puddle of useless stew, we’re going to introduce unlimited issues that exist for a reason. Or do they? That line of thinking will argue there’s actually a good use for cockroaches…

exec("SET NAMES utf8");

// Getting really desperate?  Force any string to be reencoded and experiment with the results:
$newstring = mb_convert_encoding($oldstring, 'ISO-8859-15', 'UTF-8');


Other functions to check out that may help getting rid of those pesky creatures? Check out the iconv() function.

About Author:

Senior Cloud Software Engineer and 25+ years experienced video production, video editing and 3D animation services for a variety of global clients including local video production here in Jacksonville, Florida.

Leave a Comment

Your email address will not be published. Required fields are marked *