所以我有一个单词文件。当一个人进入时,他们应该取回该词的所有字谜(如果有的话)。
示例:输入' hat'用h-a-t
检查文件中是否有3个字符所以这就是我现在的位置..
$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;
}
}
这当然不起作用,因为你不能使用数组作为针。所以我被困在如何去做。
答案 0 :(得分: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数据库(word.txt
):
post
some
pots
random
spot
words
stop
for
tops
testing
输出:
post
pots
spot
stop
tops