PHP代码返回了特殊德国字符的HTML实体 [英] PHP code is returning HTML entities for special German characters

查看:104
本文介绍了PHP代码返回了特殊德国字符的HTML实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法获取PHP 5.3代码以完全返回MySQL(5.5)数据库中的内容。

I cannot get my PHP 5.3 code to return exactly what I have in my MySQL (5.5) DB.

我正在使用jquery-ui-autocomplete(1.9 .2,但它也使用1.8.x来检索城市名称。例如,一个城市是Heßheim,但PHP返回 He & szlig; heim。数据库/表排序规则为Latin1_general_ci,但将其更改为utf8_general_ci则无济于事。我的PHP代码:

I'm using jquery-ui-autocomplete (1.9.2, but it did it with 1.8.x as well) to retrieve city names. For example one city is "Heßheim" but PHP is returning "Heßheim". The database/table collation was Latin1_general_ci, but changing it to utf8_general_ci did not help. My PHP code:

$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。

I've tried everything I could think of to decode the HTML entities, but even the above does not do it. Also the $row['city'] = htmlentities(strip_tags($row['city'])); has to be there, or it won't return anything for Heßheim.

测试脚本会返回此网页-[ Heddesheim, Heidelberg,Heßheim]-页面源代码看起来像这样-[ Heddesheim ,海德堡,他& szlig; heim]。当然,jquery-ui-autocomplete将其列为He & szlig; heim。

Testing the script returns this webpage - ["Heddesheim","Heidelberg","Heßheim"] - and the page source looks like this - ["Heddesheim","Heidelberg","Heßheim"]. Of course jquery-ui-autocomplete is listing it as Heßheim.

推荐答案

您应该使用 html_entity_decode()而不是 htmlspecialchars_decode PHPMAN

You should use html_entity_decode() instead of htmlspecialchars_decode. PHPMAN

此外,为什么只对它进行解码呢?浏览器将在渲染页面时解码实体,所以我认为您所需要的只是

Also, why are you decoding it if you just encoded it ? The browser will decode the entities while rendering the page, so I think all you need is

$row['city'] = htmlentities(strip_tags($row['city']));  
$row_set[] = $row['city'];

这篇关于PHP代码返回了特殊德国字符的HTML实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆