Question

I stumbled upon a "typo generator" script. I am attempting to modify it so that a user can enter a word and generate a list of typos.

This is the script

$str = $_POST["str"];

function getTypos($str) {
    $typosArr = array();
    $strArr = str_split($str);

    //Proximity of keys on keyboard
    $arr_prox = array();
    $arr_prox['a'] = array('q', 'w', 'z', 'x');
    $arr_prox['b'] = array('v', 'f', 'g', 'h', 'n');
    $arr_prox['c'] = array('x', 's', 'd', 'f', 'v');
    $arr_prox['d'] = array('x', 's', 'w', 'e', 'r', 'f', 'v', 'c');
    $arr_prox['e'] = array('w', 's', 'd', 'f', 'r');
    $arr_prox['f'] = array('c', 'd', 'e', 'r', 't', 'g', 'b', 'v');
    $arr_prox['g'] = array('r', 'f', 'v', 't', 'b', 'y', 'h', 'n');
    $arr_prox['h'] = array('b', 'g', 't', 'y', 'u', 'j', 'm', 'n');
    $arr_prox['i'] = array('u', 'j', 'k', 'l', 'o');
    $arr_prox['j'] = array('n', 'h', 'y', 'u', 'i', 'k', 'm');
    $arr_prox['k'] = array('u', 'j', 'm', 'l', 'o');
    $arr_prox['l'] = array('p', 'o', 'i', 'k', 'm');
    $arr_prox['m'] = array('n', 'h', 'j', 'k', 'l');
    $arr_prox['n'] = array('b', 'g', 'h', 'j', 'm');
    $arr_prox['o'] = array('i', 'k', 'l', 'p');
    $arr_prox['p'] = array('o', 'l');
    $arr_prox['r'] = array('e', 'd', 'f', 'g', 't');
    $arr_prox['s'] = array('q', 'w', 'e', 'z', 'x', 'c');
    $arr_prox['t'] = array('r', 'f', 'g', 'h', 'y');
    $arr_prox['u'] = array('y', 'h', 'j', 'k', 'i');
    $arr_prox['v'] = array('', 'c', 'd', 'f', 'g', 'b');    
    $arr_prox['w'] = array('q', 'a', 's', 'd', 'e');
    $arr_prox['x'] = array('z', 'a', 's', 'd', 'c');
    $arr_prox['y'] = array('t', 'g', 'h', 'j', 'u');
    $arr_prox['z'] = array('x', 's', 'a');
    $arr_prox['1'] = array('q', 'w');
    $arr_prox['2'] = array('q', 'w', 'e');
    $arr_prox['3'] = array('w', 'e', 'r');
    $arr_prox['4'] = array('e', 'r', 't');
    $arr_prox['5'] = array('r', 't', 'y');
    $arr_prox['6'] = array('t', 'y', 'u');
    $arr_prox['7'] = array('y', 'u', 'i');
    $arr_prox['8'] = array('u', 'i', 'o');
    $arr_prox['9'] = array('i', 'o', 'p');
    $arr_prox['0'] = array('o', 'p');

    foreach($strArr as $key=>$value) {
        $temp = $strArr;
        foreach ($arr_prox[$value] as $proximity){
            $temp[$key] = $proximity;
            $typosArr[] = join("", $temp);
        }
    }   

    return $typosArr;
}

I created a HTML page with a text box and submit button that posts the data to the above php file. How do I display the results? I have tried echo $typosArr; but this does not seem to work.

Was it helpful?

Solution

Your code never actually calls the function. The command "print_r()" would be a quick-and-dirty way to see a dump of the array. If you want more user-friendly output, try something like this:

$str = $_POST["str"];
$results = getTypos($str);
foreach($results as $result) {
   echo $result . '<br />';
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top