简体   繁体   English

特殊字符不会显示在浏览器中

[英]Special characters do not display in browser

When echoing data from the MySQL database I get strange symbols in the text. 从MySQL数据库回显数据时,文本中出现奇怪的符号。 I've tried htmlspecialchars_decode() , but to no avail. 我试过了htmlspecialchars_decode() ,但是没有用。 The data is stored as a VARCHAR in the MySQL database and is displayed as they should when queried in MySQL workbench. 数据以VARCHAR形式存储在MySQL数据库中,并在MySQL工作台中查询时按应有的方式显示。

The characters include ' , ë , è , é , ê , ... 该字符包括'ëèéê ,...

How do I get these characters to display in html? 如何获取这些字符以html显示?

屏幕截图

Have you declared a charset in the HTML of your page? 您是否在页面的HTML中声明了字符集?

In HTML5 this is fine: 在HTML5中,这很好:

<meta charset="UTF-8">

Older (HTML 4.01) needs something like: 较旧的(HTML 4.01)需要如下所示:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

You have encoding issues. 您有编码问题。

  1. You need to set <meta charset="UTF-8"> . 您需要设置<meta charset="UTF-8">

  2. You need to run query after you set up connection to DB "SET NAMES utf8" 建立与数据库“ SET NAMES utf8”的连接后,需要运行查询

  3. If you using PDO need set PDO like this new M_PDO("mysql:host={$dbhost};dbname={$dbname};charset=utf-8", $dbuser, $dbpassword); 如果您使用PDO,则需要像这样new M_PDO("mysql:host={$dbhost};dbname={$dbname};charset=utf-8", $dbuser, $dbpassword);一样设置PDO new M_PDO("mysql:host={$dbhost};dbname={$dbname};charset=utf-8", $dbuser, $dbpassword);

Also you can use this function which will encode accents to html entities 您也可以使用此功能将口音编码为html实体

function convert($str){
     return mb_convert_encoding($str, "HTML-ENTITIES", "UTF-8");
}
echo convert($YourDataFromDB);

Hope it will helps 希望这会有所帮助

This worked for me: 这对我有用:

$db = Database::getInstance();
$mysqli = $db->getConnection();
$mysqli->set_charset('utf8mb4'); 

Stolen from UTF-8 all the way through UTF-8一直偷走

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM