开发者

utf-8 problem in using jquery autocomplete tags

hey mates . rece开发者_StackOverflowntly i used jquery auto-complete tag

http://devthought.com/projects/jquery/textboxlist/

everything goes fine except utf-8 tag suggesting , only English tags are suggested

i guess something goes wrong with script lines it works fine with English tags but not with multi byte languages like Persian


Probably line 212 in the TextboxList.Autocomplete.js is to blame:

regexp = new RegExp('\\b' + escapeRegExp(search), insensitive ? 'i' : '');

That's looking for the given character after a word boundary. But word boundaries are dependent on recognition of word characters, and JavaScript RegExp's list of word characters is just the ASCII alphanumerics plus _. Because RegExp knows nothing about Unicode this won't work where the word begins with a non-ASCII character.

You could try getting rid of the \\b in which case it would match any suggestion with the given string anywhere inside it, not just at the start of words.


Your HTTP header is wrong. It should be:

header('Content-Type: application/json; charset=UTF-8');

You can also shorten your code and do the sorting with MySQL:

$sql = 'SELECT `tag` FROM `'.$prefix.'_tags` ORDER BY `tag`';
$result = $db->sql_query($sql);
if (!$result) {
    header($_SERVER['SERVER_PROTOCOL'].' 500 Internal Server Error');
    echo mysql_error();
    exit;
}

$response = array();
$i = 0;
while ($row = $db->sql_fetchrow($result)) {
    $response[] = array($i++, $row);
}
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($response);


Your content-type header is somewhat wrong. First, it should be content-type:something; charset=something, that is, content-type:text/html; charset=utf-8.

But it is actually suggested to use content-type application/json, see here What is the correct JSON content type?

So, you could do it like this

header("Content-Type:application/json; charset=UTF-8");


Probably line 215 in the TextboxList.Autocomplete.js is to blame:

if (regexp.test(values[i][1])) newvals.push(values[i]);

covert it to

if (values[i][1].indexOf(escapeRegExp(search)) != -1) newvals.push(values[i]);

Bobince (first answer) right, the problem is in line 212, but resolved it can be in line 215

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜