I need somting like this Click Here , but a want to type letters in separate <input> one <input> one letter.
My script find just words with 4 letters if i type 'g','o','l','d' he find me just 'gold' but don't find 'old' and if have the same letters.

I don't know how to write correctly if function.

Maybe I'm started a good, maybe I'm not. Pls tell me where is my fault and how i can fix that.

HTML CODE:

<form action="" method="post">
<input id="box" name="letter1" type="text" maxlength="1" >
<input id="box" name="letter2" type="text" maxlength="1" >
<input id="box" name="letter3" type="text" maxlength="1" >
<input id="box" name="letter4" type="text" maxlength="1" >
<input type="submit">
</form>

PHP CODE:

$letter1 = $_POST['letter1']; /* 1st letter */
$letter2 = $_POST['letter2']; /* 2nd letter */
$letter3 = $_POST['letter3']; /* 3rd letter */
$letter4 = $_POST['letter4']; /* 4th letter */   

$array = array('old','gold','put','road'); /* array of words */


for ($x=0; $x<sizeof($array); $x++){     

$pos1 = strpos($array[$x], $letter1); /* check 1st letter in all words */
$pos2 = strpos($array[$x], $letter2); /* check 2nd letter in all words */
$pos3 = strpos($array[$x], $letter3); /* check 3rd letter in all words */
$pos4 = strpos($array[$x], $letter4); /* check 4th letter in all words */   

if ($pos1!== false and $pos2!== false and $pos3!== false and $pos4!== false){

    echo $array[$x]."<BR>";    

    }
}

Use extra parenthesis in your if to separate the parts.

What you can also do is this:

foreach ($array as $word) {     
    if (strpos($word, $letter1) === false) continue;
    if (strpos($word, $letter2) === false) continue;
    if (strpos($word, $letter3) === false) continue;
    if (strpos($word, $letter4) === false) continue;

    echo $word . '<br/>';    
}

If you turn the letters in an array too, you can make it work easily for any length.

Hi,

Try this :

<?php

    $words = array('bold', 'old', 'board', 'gold', 'google', 'new', 'odlg');

    $letter1 = $_POST['letter1'];
    $letter2 = $_POST['letter2'];
    $letter3 = $_POST['letter3'];
    $letter4 = $_POST['letter4'];

    for($i=0; $i<count($words); $i++){
        $exist = 0;
        $word = $words[$i];
        echo 'word : '.$word;
        if(strstr($word, $letter1)) $exist++;
        if(strstr($word, $letter2)) $exist++;
        if(strstr($word, $letter3)) $exist++;
        if(strstr($word, $letter4)) $exist++;
        echo '<br>search result : '.$exist.'<br>';
        if($exist ==4) echo '====> word found : '.$word.'<br>';
        echo '---------------------------------<br>';
    }

?>
Member Avatar for diafol

I don't understand completely, but do you want to create a scrabble-like word finder?
So I assume you have a dictionary or array of words to check against, as in your example.

<?php
$letters = "";$output="";
if(isset($_POST['letters'])){
    $post_array = array_filter($_POST['letters']);
    $letters = "Letters: " . implode(",",$post_array) . "<br />";
    $num_letters = count($post_array);
    $my_words = array('old','gold','put','roady');
    foreach($my_words as $w){
        if(strlen($w) <= $num_letters){
            $r = str_split($w);
            if(count(array_intersect($r,$post_array)) == strlen($w)) $matches[] = $w;
        }
    }   
    $output = (!isset($matches)) ? "No matches found" : "Matches: "  . implode(", ",$matches);
}
?>


<html>
<body>
<form method="post">
    <input maxlength="1" name="letters[]"  />
    <input maxlength="1" name="letters[]"  />
    <input maxlength="1" name="letters[]"  />
    <input maxlength="1" name="letters[]"  />
    <input maxlength="1" name="letters[]"    />
    <input type="submit" value="go" name="subme" />
</form>

<?php echo $letters . $output;?>

</body>

</html>

Quick coding - no real testing done on it. I haven't thought about multiple single letters though.

/?EDIT

Duplicate characters IS a problem - will need to rethink...

diafol very nice, and thank you so much. I have a list of about 10.000 words and i put that in array.

But here have some problem. All letters form $post_array are multiple after foreach. I do not understand too well the function (foreach). Pls tell me how can fix that.

array is ('google','good','gol','gold');

Letters: g,o,l,d,e,w
Matches: google, good, gol, gold

should find that only "GOLD", but because multiple letters he found a much more.

Thank again.

Member Avatar for diafol

OK try this - I ditched the previous as there was a problem with duplicate chars. 10,000 is a bit big for this code perhaps. :(

<?php
//SET ALL STRINGS TO LOWERCASE
function item_to_lower($item){
    return mb_strtolower($item,'UTF-8');    
}
//COMPARE ARRAYS FUNCTION
function getMatches($post, $word){
    $sum = 0;
    foreach($word as $char=>$num){
        if(isset($post[$char]) && $post[$char] >= $num)$sum += $num;
    }
    return $sum;
}
//INITIALIZE VARIABLES
$letters        =   "";
$match_words    =   "";

//RUN CODE IF FORM SUBMITTED 
if(isset($_POST['letters'])){
    //LOAD DICTIONARY   
    $words = array('sold','gold','put','odder');
    //GET INPUT LETTERS AND PROCESS
    $post_array = array_map("item_to_lower",array_filter($_POST['letters']));
    $num_chars = count($post_array);
    //CREATE ARRAY OF CHARS WITH TOTALS
    $post_chars = array_count_values($post_array);
    ksort($post_chars);
    $free_chars = (isset($post_chars['?'])) ? $post_chars['?'] : 0;
    //LOOP OVER THE DICTIONARY FRO MATCHES
    foreach($words as $w){
        //ONLY CHECK WORDS THAT ARE SAME/SHORTER THAN NUMBER OF TILES
        if(strlen($w) <= $num_chars){
            //CLEAN UP DICTIONARY WORDS AND CREATE ARRAY OF CHARS WITH TOTALS
            $word_chars = array_count_values(str_split(item_to_lower($w)));
            //RUN FUNCTION TO MAKE THE COMPARISONS AND ADD THE '?' TILES
            $matches = getMatches($post_chars,$word_chars) + $free_chars;
            //COMPARE MATCHES TO WORD LENGTH 
            if($matches >= strlen($w))$output[] = $w;
        }
    }
    //SETUP OUTPUT  
    $letters = "Letters: " . implode(",",$post_array) . "<br />";
    $match_words = (!isset($output)) ? "No matches found" : "Matches: "  . implode(", ",$output);
}
?>

<html>
<body>
<form method="post">
<?php
    //TO RETAIN DATA SUBMITTED PREVIOUSLY IN FORM
    for($x=0;$x<7;$x++){
        $v = (isset($post_array[$x])) ? 'value = "' . $post_array[$x] . '" ' : '';  
        echo "<input maxlength=\"1\" name=\"letters[]\" $v/>";
    }
?>
    <input type="submit" value="go" name="subme" />
</form>

<?php echo $letters . $match_words;?>

</body>
</html>

me just 'gold' but don't find 'old'

Wouldn't it be easier to load a database with a dictionary and query against it.
For [my program that I'm blatantly violating the rules promoting here] that is what I did. But I query for the letters NOT in the users list. So all I get back are legit words.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.