Jump to content

Problem with htmlentities() and UTF-8 on PHP 5.2.8


steveboj

Recommended Posts

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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-

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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  :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.