我无法让PHP5.3代码准确返回MySQL (5.5) DB中的内容。
我使用jquery-ui-autocomplete (1.9.2,但在1.8.x中也是如此)来检索城市名称。例如,有一个城市是“He«heim”,但PHP返回的是"Heßheim“。数据库/表排序规则为Latin1_general_ci,但将其更改为utf8_general_ci没有任何帮助。我的PHP代码:
$term = trim(strip_tags($_GET['term']));
//retrieve the search term that autocomplete sends
if (!is_numeric($term)) {
$qstring = "SELECT DISTINCT city FROM table WHERE city LIKE '" . $term . "%' ORDER BY city ASC";
$result = mysql_query($qstring);
//query the database for entries containing the term
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { //loop through the retrieved values
//$row['id'] = (int)$row['id'];
$row['city'] = htmlentities(strip_tags($row['city']));
$row_set[] = htmlspecialchars_decode($row['city']);
//build an array
}
}
header('Content-Type: text/html; charset=utf-8');
echo json_encode($row_set);我已经尝试了我能想到的所有方法来解码HTML实体,但即使是上面的方法也做不到。此外,$row['city'] = htmlentities(strip_tags($row['city']));必须在那里,否则它不会为He?heim返回任何内容。
测试脚本会返回这个网页-- "Heddesheim","Heidelberg",“He«heim”--页面源代码看起来像这样-- "Heddesheim","Heidelberg","Heßheim“。当然,jquery-ui-autocomplete将其列为Heßheim。
发布于 2013-01-02 04:05:33
您应该使用html_entity_decode()而不是htmlspecialchars_decode。PHPMAN
另外,如果你只是编码,为什么你要解码它?浏览器会在呈现页面时对实体进行解码,所以我认为您需要做的就是
$row['city'] = htmlentities(strip_tags($row['city']));
$row_set[] = $row['city'];https://stackoverflow.com/questions/14073009
复制相似问题