在ajax中使用特殊字符

时间:2014-07-19 16:53:47

标签: javascript php jquery ajax

我正在使用“http://www.devbridge.com/sourcery/components/jquery-autocomplete/”的自动完成程序,但在之前的版本中,因为它的轻量级和速度。我使用的版本可以在这里找到:https://code.google.com/p/jquery-autocomplete/source/browse/

在其中,我尝试使用

接收结果
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

$(function() {

$("#ac1").autocomplete('search.php', {
    selectFirst: true
});

$("#flush").click(function() {
    var ac = $("#ac1").data('autocompleter');
    if (ac && $.isFunction(ac.cacheFlush)) {
        ac.cacheFlush();
    } else {
        alert('Error flushing cache');
    }
});

data.php结构非常简单:

$data = array(
"Berlin" => "10178",
"Hamburg" => "20038",
"München" => "80331",

,search.php文件如下:

<?php
include 'data.php';
include 'data.php';
function autocomplete_format($results) {
foreach ($results as $result) {
    echo $result[0] . '|' . $result[1] . "\n";
}
}


if (isset($_GET['q'])) {
$q = strtolower($_GET['q']);
if ($q) {
    foreach ($data as $key => $value) {
        if (strpos(strtolower($key), $q) !== false) {
            $results[] = array($key, $value);
        }
    }
}
}

$output = 'autocomplete';
if (isset($_GET['output'])) {
$output = strtolower($_GET['output']);
}

if ($output === 'json') {
echo json_encode($results);
} else {
echo autocomplete_format($results);
}

现在我有两个问题:

  1. 如您所见,data.php包含“ü”和“ö”等特殊字符。这些在结果中没有正确显示。任何人都可以帮助我并告诉我如何解决这个问题?

  2. 第二个问题是如何在城市名称和“=&gt;”之后回显第二部分关于搜索到的城市?

  3. 非常感谢您的帮助

1 个答案:

答案 0 :(得分:2)

我认为你需要为源自search.php的每个返回值加上utf8_decode()

这似乎有效

$results[] = array(utf8_decode($key), $value);

是的,它可以正常工作,但如果它不是用UTF-8编码并且包含未在ISO 8859字符集中定义的字符,那么使用utf8_decode()会破坏字符串。

在进行一些测试后,似乎autocomplete_format()在回显数组值之前没有设置正确的编码。另一方面,Json_encode()默认会转义为UTF-8,除非您将JSON_UNESCAPED_UNICODE(仅限PHP 5.4+)作为参数(但正确的JSON解析器不应该对unicode转义字符有问题)。

无论如何,至少在我的有限测试中,它似乎明确地设置了解决问题的编码。

<?php
include 'data.php';
include 'data.php';
function autocomplete_format($results) {
foreach ($results as $result) {
    echo $result[0] . '|' . $result[1] . "\n";
}
}


if (isset($_GET['q'])) {
$q = strtolower($_GET['q']);
if ($q) {
    foreach ($data as $key => $value) {
        if (strpos(strtolower($key), $q) !== false) {



            $results[] = array($key, $value);
        }
    }
}
}

$output = 'autocomplete';
if (isset($_GET['output'])) {
$output = strtolower($_GET['output']);
}

//set proper MIME and encoding before echoing the return value
if ($output === 'json') {
header('Content-Type: application/json; charset=utf-8');
echo json_encode($results, JSON_UNESCAPED_UNICODE);
} else {
header('Content-Type: text/plain; charset=utf-8');
echo autocomplete_format($results);
...

关于问题#2

这可以通过你已经使用的jQuery-autocomplete来完成。

假设我们有两个输入框,#ac1表示城市名称(自动完成输入),#num1表示数字值:

 <form>
    <input type="text" id="ac1">
    <input type="text" id="num1">
</form>

然后,您需要在onItemSelect

内的#ac1自动填充定义中添加<head>选项

$(function() {

$("#ac1").autocomplete('search.php', {
    selectFirst: true
}

//when autocomplete entry is selected..

{onItemSelect: function(item) {

//item = city, data = city's numeric value 
//fill the value of #num1 input box with the numeric value  

 $("#num1").val(item.data);
                                                           }},
);

. . .
});
相关问题