简体   繁体   中英

utf-8 problem in using jquery autocomplete tags

hey mates . recently 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 content-type:something; charset=something , that is, content-type:text/html; charset=utf-8 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM