从句子中随机隐藏单词


Hide word randomly from a sentence

我有一句话想随机隐藏一个单词,例如:

<?php
$sentence = 'My name is Abu Rayane';
?>

我如何继续隐藏该句子中的单词并用输入文本替换它,这样用户就可以填充它,然后检查它是否正确?例如:

My name is Abu <input type="text" name="fillBlank"> <br />
<input type="submit" name="check" value="check">
$sentence = 'My name is Abu Rayane';
$sentence = explode(' ', $sentence); 
$position = rand(0, count($sentence) - 1);
$answer = $sentence[$position];
$sentence[$position] = '<input type="text" name="fillBlank">';
echo implode(" ", $sentence);

示例:https://eval.in/185829

编辑:如果未知中的单词长度

,则使其工作

您可以使用类似的东西

$word = explode(' ', $sentence);
$word = $word[rand(0, count($sentence) - 1)];

如果要断开字符串,explode函数非常有用。还有一个函数implode,它的作用与完全相反

我试过了,我从你的不同代码中得到了答案:

<?php
$sentence = 'My name is Abu Rayane';
$countSentence = str_word_count($sentence);
echo 'Total words '.$countSentence.'<br />';
// get random number from 0 to 4
$rand = rand(0, $countSentence - 1);
// explode sentence
$ex = explode(' ',$sentence);
// get the equivalent word for a rand number
$hide = $ex[$rand];
$z = implode(' ', $ex).'<br />';
echo str_replace($hide, '______', $z);
?>

已测试:https://eval.in/185847