1
votes

Alter language settings for PHP application - WAMP server. Firefox do not regognize language in my PHP-application and replace letters in my language with very small <?>-icons (with appropriate quotes, of cause). It does not help to write <html lang=nb-NO> or <html lang=no> at the start of the PHP application file and not using <meta charset=iso-8859-1> in the header either.

The letter substitution does not occur in PHP-admin and not in a local installed Wordpress. In Wamp server 3.0.0. most text are in english. Data stored in MySQL have been stored correct. Right click on Wamp-server icon in the system tray and then the Language menu choice gives a language list with v-mark before english.

How can settings be altered so that Firefox/IE/Opera/... will interpret the application correct and display all characters in the alphabet?

1

1 Answers

2
votes

Your problem is not with the language, actually the language does not influence anything. The problem is with charset.

Commons issues in enconding

It is very common when working with accents that we find strange characters such as:

  • Like this é (é character in Unicode), this is because the character is unicode, but the page is in iso-8859-1 (or compatible).
  • The signal/character is an example of this is when you use a compatible accents with "iso-8859-1" on a page that's trying to process "UTF-8" because of the use of Content-Type: ...; charset=utf8.

What is needed to use UTF-8

  • PHP scripts (refer to files on the server and not the answer thereof) must be saved in "utf-8 without BOM"
  • Set MySQL (or other database system) with charset=utf-8
  • It is recommended to use header('Content-type: text/html; charset=UTF-8'); in PHP scripts (use a framework may not be necessary, the situation varies).

Note: The advantage of UTF-8 is that you can use various "languages" in your page with characters that are not supported by "iso-8859-1".

About ISO-8859-1

I recommend using ISO-8859-1 if your site does not use characters other than Latin and you do not need "extra encodings" (such as "icons" of "UTF-8" or "UTF-16"), however even if you do no need of UTF-8, one of the reasons that might be good to move to UTF-8, it is that in June 2004, the development group of ISO/IEC responsible for its maintenance, declared the end of support for this encoding, focusing on UCS and Unicode.

Source: http://en.wikipedia.org/wiki/ISO_8859-1

If you decide to use UTF-8 in your site, I recommend the following steps:

PHP script with UTF-8 (without BOM)

Note: read about BOM in http://en.wikipedia.org/wiki/UTF-8#Byte_order_mark

You should save all PHP scripts (yet you will use with include,require, etc.) in UTF-8 without BOM, use programs like SublimeText or notepad++ for convert files:

  • Using notepad++:

    utf8 without bom notepad++

  • Using Sublime Text:

    utf8 sublime text

  • Netbeans got to Window > Preferences > General > Workspace > Text File Encoding:

    utf8 netbeans

MySQL with UTF-8

To create a table in UTF-8 you should use something like:

CREATE TABLE mytable (
   id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
   title varchar(300) DEFAULT NULL
) ENGINE=InnoDB CHARACTER SET=utf8 COLLATE utf8_unicode_ci;

If the tables already existed, so first make a BACKUP them and then use one of the following commands (as appropriate):

  • Convert database:

    ALTER DATABASE mysdatabase CHARACTER SET utf8 COLLATE utf8_unicode_ci;
    
  • Convert specific table:

    ALTER TABLE mytable CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
    

In addition to creating the tables in UTF-8 it is necessary to define the connection as UTF-8.

With PDO use exec:

$conn = new PDO('mysql:host=HOST;dbname=mysdatabase', 'USER', 'PASSWORD');
$conn->exec('SET CHARACTER SET utf8');//set UTF-8

With mysqli use mysqli_set_charset:

$mysqli = new mysqli('HOST', 'USER', 'PASSWORD', 'mysdatabase');

if (false === $mysqli->set_charset('utf8')) {
    printf('Error: %s', $mysqli->error);
}

Setting the page charset

You can use the <meta> tag to set the charset, but recommended is you do this in the response of the request (server response), defining the "headers" (this does not mean that you should not use <meta>).

Use header function, the reason to use the server response is also because the page rendering time as the server response and page "AJAX" also need the charset defined by header();.

Note: header(); should always go at the top of the script before anyone echo ...;, print "...";, or other output function.

Example:

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

echo 'Hello World!';