douwei8672 2013-10-25 10:02
浏览 50
已采纳

自动提供Javascript / PHP / AJAX代码

Consider an array holding some values

array = {'microsoft','micromax', 'miniclip','michael jackson','million','milky way'}

When an user start typing the text, say s/he is trying to enter million. When user start typing 'mi' the above suggestion in the array will be shown to the user.

My Question:

Let us assume user is typing the word 'mini clip', by typo s/he started to type 'mni' or 'minc' or 'nim' or 'imn' or 'nim' instead of 'min', this scenario also need to show the suggestion to the user. Becasue, anyway those typed characters are available in the word 'miniclip'. Typo is common for all entry level users/common users. So I need code in javascript/php/ajax/opensource library to suit this scenario.

  • 写回答

2条回答 默认 最新

  • donglang9880 2013-10-25 12:58
    关注

    HTML

    <form action=""><input type="text" name="word" id="word"></form>
    <div id="auto"></div>
    

    JS

    $(function(){
        $('#word').keyup(function(e){
            var input = $(this).val();
            $.ajax({
                type: "get",
                url: "autocomplete.php",
                data: {word: input},
                async: true,
                success: function(data){
                    var outWords = $.parseJSON(data);
                    $('#auto').html('');
    
                    for(x = 0; x < outWords.length; x++){
                        $('#auto').prepend('<div>'+outWords[x]+'</div>'); //Fills the #auto div with the options
                    }
                }
            })
        })
    });
    

    Don't forget to link jQuery...

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    

    N.B.

    You would need to do something like add an onclick event to the child divs of #auto to replace the contents of #word (the input field).

    PHP

    $array  = array('microsoft','micromax', 'miniclip','michael jackson','million','milky way');
    $input  = urldecode($_GET['word']); //Get input word/phrase (decode in case of spaces etc.)
    $length = strlen($input);           //Get length of input word
    // $min    = $length - 1;              //Length of word - 1
    // $max    = $length + 1;              //Length of word + 1
    
    $returned = preg_grep('/^(['.$input.']{'.$length.'}.*)$/i', $array); //Find matches in $array and return as array
    $returned = array_values($returned);                                //Re-index from 0
    
    echo json_encode($returned); //Returm json string to ajax call
    

    Regex

    /^([$input]{$length}.*)$/i
    
    1. / => Starting delimter
    2. ^ => Start of string
    3. ( => Start a capture group
    4. [ => Start a character class
    5. $input => Add the input word to the character class
    6. ] => End the character class (4)
    7. {$length} => Set length of string to match character class against (length of input word)
    8. .* => Match any following characters 0 or more times
    9. ) => End capture group (3)
    10. $ => Match end of string
    11. / => Ending delimeter
    12. i => Modifier for case insensitivity

    Min/Max

    I have included the commented $min and $max variables... An added feature that I thought you might like... You would implement them by changing:

    {'.$length.'}          <-- Change this
    {'.$min.','.$max.'}    <--To that
    {'.$length.','.$max.'} <-- Or that (or another combination)
    

    Example

    An example might best show how this works...

    Suppose an auto correct array of:

    $array = array('loser', 'loses', 'losing');
    

    and an input of:

    lose
    

    Currently ({'.$length.'}) the code will return:

    loser
    loses
    

    But if you change it to {'.$min.','.$max.'} (and uncomment $min / $max); it will return:

    losing
    loser
    loses
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog