jQuery自动完成特殊字符(挪威语)问题 [英] jQuery autocomplete special character (Norwegian) problems

查看:111
本文介绍了jQuery自动完成特殊字符(挪威语)问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的挪威网站上使用jQuery的自动完成功能.输入挪威语字符æ,ø和å时,自动完成功能会建议带有相应字符的单词,但不建议以相应字符开头的单词.看来我必须设法在单词中间对挪威语字符进行字符编码,而不要对以该字符开头的字符进行编码.

I'm using jQuery's autocomplete function on my Norwegian site. When typing in the Norwegian characters æ, ø and å, the autocomplete function suggests words with the respective character, but not words starting with the respective character. It seems like I've to manage to character encode Norwegian characters in the middle of the words, but not characters starting with it.

我正在使用具有自己功能的PHP脚本将挪威语字符编码为UTF-8并生成自动完成列表.

I'm using a PHP script with my own function for encoding Norwegian characters to UTF-8 and generating the autocomplete list.

这真令人沮丧!

代码:

PHP代码:

$q = strtolower($_REQUEST["q"]);
if (!$q) return;

function rewrite($string){

 $to = array('%E6','%F8','%E5','%F6','%EB','%E4','%C6','%D8','%C5','%C4','%D6','%CB', '%FC', '+', ' ');
 $from = array('æ', 'ø', 'å', 'ä', 'ö', 'ë', 'æ', 'ø', 'å', 'ä', 'ö', 'ë', '-', '-');

 $string = str_replace($from, $to, $string);

 return $string;
}

$items是一个包含建议词的数组.

$items is an array containg suggestion-words.

foreach ($items as $key=>$value) {
  if (strpos(strtolower(rewrite($key)), $q) !== false) {
    echo utf8_encode($key)."\n";
  }
}

jQuery代码:

$(document).ready(function(){
$("#autocomplete").autocomplete("/search_words.php", {
        position: 'after',
        selectFirst: false,
        minChars: 3,
        width: 240,
        cacheLength: 100,
        delay: 0
        }
    )
}
);

推荐答案

感谢您提供所有答案和帮助.我当然学到了有关PHP和编码的新知识:)

Thank you for all answers and help. I certainly learned some new things about PHP and encoding :)

但是对我有用的解决方案是:

But the solution that worked for me was this:

我发现jQuery自动完成功能实际上将UTF-8编码并小写特殊字符,然后再将其发送到PHP函数.因此,当我写出建议内容的数组时,我使用了rewrite()函数对特殊字符进行编码.因此,在我的比较功能中,我只需要小写所有内容.

I found out that the jQuery autocomplete function actually UTF-8 encodes and lowercase special character before sending it to the PHP function. So when I write out the arrays of suggest content, I used my rewrite()-function to encode the special characters. So in my compare function I only had to lowercase everything.

现在效果很好!

这篇关于jQuery自动完成特殊字符(挪威语)问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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