2013-12-24 3 views
2

그래서 나는 단어 파일을 가지고 있습니다. 어떤 사람이 들어가면, 그 단어의 모든 아나그램을 돌려 주어야합니다.텍스트 파일에서 모든 anagram 단어 찾기

예 : 당신은을 사용할 수 없기 때문에 그렇게 물론 내가 지금에 나는 곳 ..

$wordlist=file_get_contents('words.txt'); 
    $word = $_POST['word']; 

    $word_split = str_split($word); 

    foreach($wordlist as $line){ 
     if(strpos($line, $word_split) !== false){ 
      echo $line; 
     } 
    } 

이 작동하지 않습니다

모자와 함께 아무것도 3 자 파일을 확인 '모자'를 입력 바늘로 배열. 그래서 나는 그것을하는 방법에 붙어있다.

+1

이것은 intereset 일 수 있습니다 : http://stackoverflow.com/questions/15045640/how-to-check-if-two-words-are-anagrams-in-java – Andersnk

+0

['count_chars()'] (http : //php.net/count_chars) 함수가 유용 할 것입니다. –

답변

0

아래의 설명에 문제가있는 경우 (테스트를 쉽게하기 위해 $ _GET으로 입력을 변경했습니다).

//Check first if there's any word input 
if(isset($_GET['word'])){ 
    $wordlist = explode("\n", file_get_contents('words.txt')); 
    $word = $_GET['word']; 
    $word_split = str_split($word); 
    //print_r($word_split); 
    $word_length = strlen(trim($word)); 
    foreach($wordlist as $line){ 

     $line_split = str_split(rtrim($line)); 
     $line_length = strlen(trim($line)); 
     //Check if the lengths are equal 
     $diff = array_filter($word_split, function ($val) use (&$line_split) { $key = array_search($val, $line_split);if ($key === false) return true;unset($line_split[$key]);return false;}); 

     if($line_length == $word_length && empty($diff)){ 
      echo $line."\n"; 
     } 
    } 
}else{ 
    die("No word input."); 
} 

입력 단어 : post

워드 데이터베이스 (word.txt) :

post 
some 
pots 
random 
spot 
words 
stop 
for 
tops 
testing 

출력 :

post 
pots 
spot 
stop 
tops 
+0

고마워요, 당신의 솔루션은 가깝 습니다만, 단어가 같은 길이인지 아닌지 확인하기 위해 파란색 글씨를 입력하면 동일한 글자가 포함되지 않습니다. 내가 묻히면 – Andy

+0

그 밖에 무엇을해야합니까? –

+0

그래서 모자를 찾고 있다면 문자 h-a-t를 가진 단어를 찾아야합니다 – Andy

관련 문제