1

我一直在这里阅读很多答案,但无论我尝试什么,我都无法解决这个问题。

问题

我有正在导入数据库的数据。此数据具有特殊字符,例如 ' “ ” - é(但不仅限于这些字符)。

显示数据时,它们显示为黑色菱形。

我试过的

我遵循了这个: http: //kunststube.net/frontback/但是当我进行导入时,它只是在第一个 ' 处中断并忽略字符串的其余部分(仍然正确插入)。

我尝试使用 转换字符串utf8_encode(),我尝试过htmlentities()并且尝试过使用mb_convert_encoding()所有不同的结果,但实际上并没有完全解决问题,有些删除了一些字符,有些在 IE 上给出了 lil 方块等。

我认为问题是

我认为问题在于不知道原始编码,所以我运行mb_detect_encoding()它并没有返回任何内容 - 那是什么意思?我猜它无法检测到编码。

所以我正在努力解决的是如何在不破坏字符串的情况下将其编码为 utf8,以便我可以正确存储它。

观察

如果我设置header('Content-Type: text/html; charset=utf-8');我们会得到黑色菱形,但如果我设置header('Content-Type: text/html; charset=ISO-8859-1');它会正确显示。

所以知道 - 我应该在 ISO-8859-1 中显示我的整个网站还是应该将该字符串转换为 utf8 .. 是否有偏好如何做到这一点?

当数据库是 latin1 并且我没有在 PDO 连接中包含字符集时,数据已正确存储在数据库中

其他

我正在使用 PDO

new PDO("mysql:host=" . $G['PDO_HOST'] . ";dbname=" . $G['PDO_DB'] . ";charset=utf-8", $G['PDO_USER'], $G['PDO_PASS'],array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));

问题字符串的副本:

Informed by his eclectic background and varied passions for décor, travel, entertaining and food, Nathan Turner’s American Style will appeal to readers looking to incorporate Turner’s stylish and relaxed aesthetic into their home and life.

Any input on this would be really appreciated - been struggling for a while on this

UPDATE

Here is my table

CREATE TABLE IF NOT EXISTS `page` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL,
  `url` varchar(255) NOT NULL,
  `summary` text NOT NULL,
  `content` text NOT NULL,
  `search` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=937 ;

So the table is utf8 format.

I have changed the DB connection to:

$dbc = new PDO("mysql:host=" . $G['PDO_HOST'] . ";dbname=" . $G['PDO_DB'], $G['PDO_USER'], $G['PDO_PASS']);
$dbc->query("SET NAMES utf8");

As "Your Common Sense" pointed out about the PHP version.

But now I have this all set, it cuts of the insert at the first ’</p>

String: With a style that is accessible and chic, Turner’s aesthetic is Nate meets Colin and the Magazine.

Stored: With a style that is accessible and chic, Turner

UPDATE 2

I am using prepared statements.. so the content that is breaking is here:

$stmt->bindParam(':content',$content, PDO::PARAM_STR);
4

2 回答 2

1
charset=utf8
           ^ should be NO dash here

Also, if your PHP version below 5.3.6, it won't work anyway, SET NAMES utf8 query have to be run after connect.

As for the inserts, them doesn't cut anything. It's your HTML fields.

to output an HTML attribute, always use htmlspecialchars with ENT_QUOTES flag.

于 2013-03-14T12:45:17.057 回答
-1

In database set the particular field of the collation type as 'utf8_general_ci' in the table.

于 2013-03-14T13:25:12.787 回答