How to Display Unicode Data with PHP

How to display Unicode data with PHP

Try to set charachter encoding after mysql_connect function like this:

 mysql_query ("set character_set_client='utf8'"); 
mysql_query ("set character_set_results='utf8'");

mysql_query ("set collation_connection='utf8_general_ci'");

Print Unicode characters PHP

It looks like you have UTF-8 encoded strings internally, PHP outputs them properly, but your browser fails to auto-detect the encoding (it decides for ISO 8859-1 or some other encoding).

The best way is to tell the browser that UTF-8 is being used by sending the corresponding HTTP header:

header("content-type: text/html; charset=UTF-8");  

Then, you can leave the rest of your code as-is and don't have to html-encode entities or create other mess.

If you want, you can additionally declare the encoding in the generated HTML by using the <meta> tag:

  • <meta http-equiv=Content-Type content="text/html; charset=UTF-8"> for HTML <=4.01
  • <meta charset="UTF-8"> for HTML5

HTTP header has priority over the <meta> tag, but the latter may be useful if the HTML is saved to HD and then read locally.

UTF-8 all the way through

Data Storage:

  • Specify the utf8mb4 character set on all tables and text columns in your database. This makes MySQL physically store and retrieve values encoded natively in UTF-8. Note that MySQL will implicitly use utf8mb4 encoding if a utf8mb4_* collation is specified (without any explicit character set).

  • In older versions of MySQL (< 5.5.3), you'll unfortunately be forced to use simply utf8, which only supports a subset of Unicode characters. I wish I were kidding.

Data Access:

  • In your application code (e.g. PHP), in whatever DB access method you use, you'll need to set the connection charset to utf8mb4. This way, MySQL does no conversion from its native UTF-8 when it hands data off to your application and vice versa.

  • Some drivers provide their own mechanism for configuring the connection character set, which both updates its own internal state and informs MySQL of the encoding to be used on the connection—this is usually the preferred approach. In PHP:

    • If you're using the PDO abstraction layer with PHP ≥ 5.3.6, you can specify charset in the DSN:

       $dbh = new PDO('mysql:charset=utf8mb4');
    • If you're using mysqli, you can call set_charset():

        $mysqli->set_charset('utf8mb4');       // object oriented style
      mysqli_set_charset($link, 'utf8mb4'); // procedural style
    • If you're stuck with plain mysql but happen to be running PHP ≥ 5.2.3, you can call mysql_set_charset.

  • If the driver does not provide its own mechanism for setting the connection character set, you may have to issue a query to tell MySQL how your application expects data on the connection to be encoded: SET NAMES 'utf8mb4'.

  • The same consideration regarding utf8mb4/utf8 applies as above.

Output:

  • UTF-8 should be set in the HTTP header, such as Content-Type: text/html; charset=utf-8. You can achieve that either by setting default_charset in php.ini (preferred), or manually using header() function.
  • If your application transmits text to other systems, they will also need to be informed of the character encoding. With web applications, the browser must be informed of the encoding in which data is sent (through HTTP response headers or HTML metadata).
  • When encoding the output using json_encode(), add JSON_UNESCAPED_UNICODE as a second parameter.

Input:

  • Browsers will submit data in the character set specified for the document, hence nothing particular has to be done on the input.
  • In case you have doubts about request encoding (in case it could be tampered with), you may verify every received string as being valid UTF-8 before you try to store it or use it anywhere. PHP's mb_check_encoding() does the trick, but you have to use it religiously. There's really no way around this, as malicious clients can submit data in whatever encoding they want, and I haven't found a trick to get PHP to do this for you reliably.

Other Code Considerations:

  • Obviously enough, all files you'll be serving (PHP, HTML, JavaScript, etc.) should be encoded in valid UTF-8.

  • You need to make sure that every time you process a UTF-8 string, you do so safely. This is, unfortunately, the hard part. You'll probably want to make extensive use of PHP's mbstring extension.

  • PHP's built-in string operations are not by default UTF-8 safe. There are some things you can safely do with normal PHP string operations (like concatenation), but for most things you should use the equivalent mbstring function.

  • To know what you're doing (read: not mess it up), you really need to know UTF-8 and how it works on the lowest possible level. Check out any of the links from utf8.com for some good resources to learn everything you need to know.

php displays unicode escaped characters

The problem is that the rendered json data contains escaped unicode characters like 'Rungestra\u00dfe 20'

Why is that a problem? \u00DF is perfectly valid JSON syntax for the letter ß. Any JSON decoder will parse that string literal escape into the single character U+00DF Latin Small Letter Sharp S. For example if your client is a web browser then JSON.parse().

ß can be included in an HTML document as-is, you do not have to escape it to ß or ß.

php mysql and unicode

You need to set the charset on the adapter so it doesn't corrupt the characters before trying to insert them. Since you're using MySQLi as your database adapter, you'd be looking for one of the following solutions:

// Procedural style
mysqli_set_charset($link, "utf8");

// Object oriented style
$mysqli->set_charset("utf8");

See the manual entry for set_charset here:
http://php.net/manual/en/mysqli.set-charset.php


You can also run an initial query SET NAMES utf8.

Printing unicode character

Figured it:

echo html_entity_decode('₹', ENT_NOQUOTES, 'UTF-8');

how to display php unicode charaters

Since you use MySQLi you should use the MySQLi charset command, instead of the old MySQL charset:

mysqli_set_charset ( $con , 'utf8' )



Related Topics



Leave a reply



Submit