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

Dani AI

Generated

A short, practical checklist and fixes to get English + Hindi working reliably (builds on 's baseline).

Start with a single Unicode strategy: storage, transport and output must all use a full Unicode charset (prefer utf8mb4). Set the connection charset in code (don’t rely only on server defaults), send an explicit HTTP Content-Type header, and use a modern HTML meta tag. Use a multibyte-aware PHP API for string operations.

Example PHP (mysqli + header):

<?php
$mysqli = new mysqli('dbhost','dbuser','dbpass','dbname');
if ($mysqli->connect_errno) { /* handle error */ }
$mysqli->set_charset('utf8mb4');    // set connection charset explicitly
header('Content-Type: text/html; charset=utf-8');
?>

Convert existing schema (example):

ALTER DATABASE mydb CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
ALTER TABLE mytable CONVERT TO CHARACTER SET utf8mb4 COLLATE = utf8mb4_unicode_ci;

Note: switching to utf8mb4 can trigger index-length issues on older MySQL. Options: shorten indexed VARCHAR lengths, set ROW_FORMAT=DYNAMIC, or enable the appropriate InnoDB options depending on server version.

Troubleshooting checklist (common causes of mojibake)

  • Confirm PHP/HTML/JS files are saved as UTF-8 without a BOM (a BOM can inject stray bytes or break headers).
  • Verify HTTP header and the HTML meta use UTF-8 (modern meta: <meta charset="utf-8">).
  • Ensure the DB column and the live connection charset match (mysqli_set_charset or PDO DSN charset).
  • Avoid blindly re-encoding: functions like utf8_encode convert ISO-8859-1 to UTF-8 and will corrupt already-UTF-8 text.
  • Use mbstring/PCRE with the UTF-8 flags (mb_strlen, mb_substr, PCRE /u) when manipulating text.
  • Prefer mysqli or PDO (prepared statements) over the old mysql_* extension.

Reference for the original pointers; the above expands on connection, schema conversion, multibyte handling and common pitfalls for Hindi/Devanagari plus ASCII content.

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.