Broken text editor and Word Unscramble code not showing desired result

I'm trying to make a messy two letter word.

This is in the clock.txt file

to
at
on
of
go
no
so
..
..

      

This is my html code:

<form action="" method="get">    
    <input type="text" name="word" placeholder="Write jumbled word" maxlength="2" ><br>
    <button>Find</button>
</form>

      

This is my php code:

if(isset($_GET['word'])){
    $word_one = $_GET['word'] ;
    $word_two = strrev($word_one) ;

    $file = file("clock.txt");

    $i = 0;

    foreach( $file as $value) {    
        if(($value == $word_one ) || ($value == $word_two) ) {
            $result[$i] = $value ;
            $i = $i + 1 ;
        }
    }

    if(isset($result)){
        print_r($result);
    }    
} // end if

      

This is output:

Array ()

      

But my desired result is:

Array ( [0] => go )

      

+3


source to share


1 answer


trim()

your value before comparison.

Replace your php code

if(isset($_GET['word'])){
$word_one = trim($_GET['word']) ;
$word_two = strrev($word_one) ;

$file = file("clock.txt");

$i = 0;

foreach( $file as $value){

 if((trim($value) == $word_one ) || (trim($value) == $word_two) ){
  $result[$i] = $value ;
  $i = $i + 1 ;
 }
}



if(isset($result)){
 print_r($result);
}

} // end if

      



Optional: If you want to create a messy word solver for more than two characters, you will have to rearrange your jumbled letter using the permutation formula. After each permutation, the result will match your words in the database. This process will be performed for all possible permutations. You will then be able to show all possible answers.

I did the same on my site:

http://asifiqbalkhulna.com/

+2


source







All Articles