Recipe 19.11. Setting the Character Encoding of Outgoing Data

19.11.1. Problem

You want to make sure that browsers correctly handle the UTF-8-encoded text that your programs emit.

19.11.2. Solution

Set PHP's default_encoding configuration directive to utf-8. This ensures that the Content-Type header PHP emits on HTML responses includes the charset=utf-8 piece, which tells web browsers to interpret the page contents as UTF-8 encoded.

19.11.3. Discussion

Setting default_encoding gives web browsers a heads-up that your page contents should be interpreted as UTF-8 encoded. However, you still have the responsibility to make sure that the page contents really are properly UTF-8 encoded by using string functions appropriately. 19.13 details how to do that.

If you can't change the default_encoding configuration directive, send the proper Content-Type header yourself with the header( ) function, as shown in Example 19-25.

Setting character encoding

<?php header('Content-Type: text/html;charset=utf-8'); ?>

19.11.4. See Also

19.13 for information on generating UTF-8-encoded text.

PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: