steveboj Posted December 20, 2009 Share Posted December 20, 2009 I thought I'd finally figured out how to work with UTF-8 data and htmlentities(), until I encountered PHP 5.2.8... This code works perfectly on PHP 5.2.6, but returns an empty result on PHP 5.2.8 echo htmlentities($input_text, ENT_NOQUOTES, "UTF-8"); The $input_text comes from a MySQL database. I've done some Googling and it sounds as though this is caused by characters in the $input_text that aren't actually UTF-8. Has anyone else had this problem and found a solution? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 20, 2009 Share Posted December 20, 2009 An empty result would imply that $input_text is empty (anything that is not a html entity would be left as is.) Have you checked what is in $input_text. Using ENT_NOQUOTES would leave single and double quotes as is and could easily be breaking your HTML, resulting in no apparent rendered output. Have you done a "view source" of the output in your browser so that you know what it might actually be? Quote Link to comment Share on other sites More sharing options...
ChemicalBliss Posted December 20, 2009 Share Posted December 20, 2009 Hmm, If the above reply doesn't help you, try this function: <?php // Fixes the encoding to uf8 function fixEncoding($in_str) { $cur_encoding = mb_detect_encoding($in_str) ; if($cur_encoding == "UTF-8" && mb_check_encoding($in_str,"UTF-8")) return $in_str; else return utf8_encode($in_str); } // fixEncoding ?> mb_detect_encoding() is useful in determining the current encoding, Which can help you debug easier - if it is the fact that the string was not UTF-8. Hope this Helps. -CB- Quote Link to comment Share on other sites More sharing options...
steveboj Posted December 20, 2009 Author Share Posted December 20, 2009 Thanks ChemicalBliss, using your function does make the text appear, but it also changes some of the non-standard characters to squares. I've done some testing with a different MySQL database and discovered the problem isn't actually a result of using PHP 5.2.8, e.g. The code works with PHP 5.2.8 and MySQL 5.0.51a, but it doesn't work with PHP 5.2.8 and MySQL 5.1.30 So I'm guessing it may be a character set or configuration issue with MySQL 5.1.30 - or the combination of PHP 5.2.8 and MySQL 5.1.30. Does this sound familiar to anyone else? Quote Link to comment Share on other sites More sharing options...
printf Posted December 20, 2009 Share Posted December 20, 2009 It could be your database connection client, after you connect to your database, do you set your connection client to use the same character set that your database is using to store it's data? After you connect its best to do that if your database configuration file does not have those setting set at start up... // connect to database // select database // set your character set, how the database will return the data to your script mysql_query ( "SET NAMES utf8", $connection ); // do whatever To do it automatically at MySQL start up you would add this to your MySQL configuration file under [mysqld] skip-character-set-client-handshake collation_server=utf8_unicode_ci character_set_server=utf8 If you are using a different character set then change the values to what you are using, but I recommend you always use utf8 as it maps to all known character sets. Quote Link to comment Share on other sites More sharing options...
steveboj Posted December 20, 2009 Author Share Posted December 20, 2009 It could be your database connection client, after you connect to your database, do you set your connection client to use the same character set that your database is using to store it's data? After you connect its best to do that if your database configuration file does not have those setting set at start up... // connect to database // select database // set your character set, how the database will return the data to your script mysql_query ( "SET NAMES utf8", $connection ); // do whatever Yes!!! This has solved it. I had come across "SET NAMES" before, but for some reason thought you only used it when you were inserting data into a database... Thanks for taking the time to explain this – I got so excited when I realised it was working, I didn't notice my dinner was burning Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.