1

We have something like this:

switch ($name)
{
    /** Prune **/

    case 'prune':
        echo 'Prune command: Deletes ALL of the messages from the chat.';
    break;

    /** Lock **/

    case 'lock':
        echo 'Lock command: Locks the chat, nobody can talk!';
    break;

    /** Unlock **/

    case 'unlock':
        echo 'Unlock command: Unlocks the chat, everybody can talk!';
    break;  

    /** Broadcast **/

    case 'broadcast':
        echo 'Broadcast command: Send a general broadcast message.';
    break;

    /** If command not found, echo suggestions.. **/

    default:
        echo 'Command not found, try these: /prune, /lock, /unlock, /broadcast';
    break;
}

We have a form that sends data, this data will be stored into a variable $name.

A user stored the following word: prunne.

Obviously, it will go to case default.

But what I want to do is, IF word is similar to any of the available cases, suggest the user.

For example, case prunne,

will echo: Not found, did you mean Prune*?

How do I do it?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Jony Kale
  • 979
  • 3
  • 15
  • 35
  • You might want to check out the PHP implementation of the levenshtein algorithm, http://www.php.net/manual/en/function.levenshtein.php anyway your need a whole range of permutations for each case value, or your could do what your doing and make your user fix there mistake. or use a **list box** where they choose from the valid commands – Lawrence Cherone Jun 22 '13 at 20:35
  • IMO, do not overextend. Just list all the possible commands to user, let him choose and write correctly. – sybear Jun 22 '13 at 20:35

1 Answers1

1

I can point you in the right direction:

http://php.net/manual/en/function.levenshtein.php

Is a php function that can be used for your cause, there is a nice example on that page as well.

A related stack overflow question has more info on when your strings are longer than 255 characters:

String similarity in PHP: levenshtein like function for long strings

Community
  • 1
  • 1
Bas
  • 123
  • 2
  • 7