How to handle UTF-8 characters in HTML, JavaScript, PHP and MySQL ...

Recommended Answers

All 2 Replies

Hey.

HTML:

<!DOCTYPE html>
<html>
    <head>
      <meta http-equiv="content-type" content="text/html; charset=utf8">
    </head>
    <body />
</html>

MySQL

-- When creating a table:
CREATE TABLE `myTable`(
  -- <insert fields>
)DEFAULT CHARSET=utf8;

-- Before querying the database.
-- Note, this is usually not needed, but can be.
SET NAMES 'utf8';

And as to PHP and Javascript...
You usually don't have to worry much about the character encoding in either of those. Just make sure you create the code files themselves as UTF-8 (without BOM) and you should be fine.
PHP strings are basically just byte arrays, so they don't differentiate between charsets, unless you attempt to manipulate them using the string functions, in which case they are treated as ISO. (Unless the function offers the ability to specify a charset.)

However, if PHP is giving you trouble, you can use utf8_encode to convert ISO characters over to UTF-8.
Also, if you use htmlentities or htmlspecialchars, make sure you set the third parameter correctly.

nice info Atli... i guess it will prove helpful fr me someday

Hey.

HTML:

<!DOCTYPE html>
<html>
    <head>
      <meta http-equiv="content-type" content="text/html; charset=utf8">
    </head>
    <body />
</html>

MySQL

-- When creating a table:
CREATE TABLE `myTable`(
  -- <insert fields>
)DEFAULT CHARSET=utf8;

-- Before querying the database.
-- Note, this is usually not needed, but can be.
SET NAMES 'utf8';

And as to PHP and Javascript...
You usually don't have to worry much about the character encoding in either of those. Just make sure you create the code files themselves as UTF-8 (without BOM) and you should be fine.
PHP strings are basically just byte arrays, so they don't differentiate between charsets, unless you attempt to manipulate them using the string functions, in which case they are treated as ISO. (Unless the function offers the ability to specify a charset.)

However, if PHP is giving you trouble, you can use utf8_encode to convert ISO characters over to UTF-8.
Also, if you use htmlentities or htmlspecialchars, make sure you set the third parameter correctly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.