First check default charset value set UTF-8 in phpinfo. If not found or default_charset value is not set ‘UTF-8’, then need to change in php.ini file. In php.ini if value exist with other value change to ‘UTF-8’ or if not exist add default_charset = “UTF-8”. Sometimes, default_charset value is disabled in file, in that case remove # from front to enable.
Set on web header charset value as below. Use the header() function before generating any content as below:
header ('Content-Type: text/html; charset=utf-8');
Set in meta-tag in the header:
The prior version of PHP 5.4.0 default charset is ISO-8859-1. From PHP 5.4.0 default value is UTF-8. In any case, if default charset is not UTF-8 and you want to use UTF-8 for some string or text, use htmlspecialchars function. If string or text not displayed properly on webpage and you want to convert some predefined characters to HTML entities, then use below mentioned function:
htmlspecialchars($str, ENT_NOQUOTES, "UTF-8");
XHTML parsers do not recognize the encoding declarations in meta tags. For that need XML declaration only as below:
Often found that after setting all charset in the page we found the issue in some string/text which is from SQL server. For the issue in MySQL, set default character for all MySQL connections.
$conn = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8', $conn);
For PHP 5.5.0 mysqli connections:
$conn = new mysqli('localhost', 'user', 'password', 'database');
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* change character set to utf8 */
if (!$mysqli->set_charset("utf8")) {
printf("Error loading character set utf8: %s\n", $mysqli->error);
} else {
printf("Current character set: %s\n", $mysqli->character_set_name());
}
$mysqli->close();