I have a list of about 200 words that I basically want to have user input check against and then output the unscrambled word

e.g.: if i enter 'ebraze' it should come back with 'zebra'

there is no case of multiple possible outcomes for the input letters...

this is beyond my simple mind so anything would definitely be a HUGE help
(besides obviously having all the words in a list or array... duh)

thanks!

    seems a bit more involved than i need

    would it be possible to say do something like this:

    $input = //userinput
    $words = array(..words..);
    
    foreach ($words as $word)
    {
         if (preg_match("/$input/",$word,$match))
         {
              echo $match;
         }
    }
    

      well not sure how elegant this is... but it works:

      $words = //array of words
      $input = //user input
      
      foreach($words as $word)
      {
      	if(strlen($word) == strlen($input))
      	{
      		$tmpArray[] = $word;
      	}	
      }
      if(count($tmpArray) == 1)
      {
      	$result = $tmpArray[0];
      }
      else
      {
      	$counter = count($tmpArray);
      	for($i=0;$i<count($tmpArray);$i++)
      	{
      		$found = FALSE;
      		for($j=0;$j<strlen($input);$j++)
      		{
      			$pos = strpos(strtolower($tmpArray[$i]),strtolower($input[$j]));
      
      		if($pos === FALSE)
      		{
      			$found = TRUE;				
      		}
      	}
      	if ($found)
      	{
      		$tmpArray[$i] = '';
      		$counter--;
      	}
      	else
      	{
      		$result = $tmpArray[$i];
      	}
      }
      }
      if(empty($result))
      {
      	echo "No results";
      }
      else
      {
      	echo $result;
      }
      

        Won't claim this is any better/worse:

        <?php
        /**
         * sort characters in string
         * @return string
         * @param string $word
         */
        function sortWord($word)
        {
           $letters = str_split($word);
           sort($letters);
           return implode('', $letters);
        }
        
        /**
         * look for anagram of word in array of words
         * @return bool true = found
         * @param string $word
         * @param array $wordArray
         */
        function findWord($word, $wordArray)
        {
           $word1 = sortWord($word);
           foreach($wordArray as $word2)
           {
              if($word1 == sortWord($word2))
              {
                 return true;
              }
           }
           return false;
        }
        
        // Example
        $wordList = array('one', 'two', 'three');
        var_dump(findWord('noe', $wordList)); // true
        var_dump(findWord('xyz', $wordList)); // false
        ?>
        

          its certainly ALOT shorter... i wonder how many milli-nano-infanteciamal seconds quicker it runs

          definitely a clean way to go about it

            Write a Reply...