Search code examples
phpcsstext-formatting

Web page text display problem


I keep getting these weird text characters when I display user submitted text. like in the following example below. Is there a way I can fox this using PHP, CSS or something so that the characters are displayed properly?

Here is the problem text.

Problems of �real fonts� on the web. The one line summary: 
different browsers and different platforms do �hinting�

Here is my meta tag.

<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

Solution

  • It's an encoding problem. Make sure you send the correct encoding to the browser. If it's UTF-8, you'll do it like this:

    header("Content-type: text/html; charset=utf-8");
    

    Also, make sure that you store the content using the same encoding throughout the entire system. Set your database tables to utf8. If you're using MySQL, run the SET NAMES utf8 query when connecting to make sure you're running in UTF-8.

    These weird characters occur when you suddenly switch encoding.

    Also, some functions in PHP take a $charset parameter (e.g. htmlentities()). Make sure you pass the correct charset to that one as well.

    To make sure that PHP handles your charset correctly in all cases, you can set the default_charset to utf-8 (either in php.ini or using ini_set()).