[Solved] Special character PHP HTML


It’s important that your entire line code has the same charset to avoid issues where characters displays incorrectly.

There are quite a few settings that needs to be properly defined and I’d strongly recommend UTF-8, as this has most letters you would need (Scandinavian, Greek, Arabic).

Here’s a little list of things that has to be set to a specific charset.

Headers

Setting the charset in both HTML and PHP headers to UTF-8

  • PHP: header('Content-Type: text/html; charset=utf-8');
    (PHP headers has to be placed before any kind output (echo, whitespace, HTML))

  • HTML: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    (HTML-headers are placed within the <head> / </head> tag)

File-encoding

It might also be needed for the file itself to be UTF-8 encoded. If you’re using Notepad++ to write your code, this can be done in the “Format” drop-down on the taskbar. You should use UTF-8 w/o BOM (see this SO).

Other

  • Some specific functions have the attribute of a specific charset, and if you are using such functions, it should be specified there as well. Read the documentation for each function.

Should you follow all of the pointers above, chances are your problem will be solved. If not, you can take a look at this StackOverflow post: UTF-8 all the way through.

1

solved Special character PHP HTML