文件中每行前面的随机单词


Random Word in Front of Each Line in a File

我想在每行前面添加一个随机文本,像这样:

:

One
Two
Three
:后

Love One
My Two
Other Three

更新:对不起的混乱,我的意思是"之前"文本是一个文本区域提交的文本,所以它在$_POST值,我想要的结果像"之后"的文本。简单地说,代码如下:让我们假设之前的文本在$_POST['message']值下,我想要echo值,但在它前面有随机文本。

我尝试了这个,但只适用于第一行,而不是其他行:

$rand = array("Love", "My", "Other");
$message = trim(@$_POST['message']) ;
$message = str_replace(" ","+",$message);//Convert the space to +
$modifiedTextAreaText = str_replace( "'n", "'n$rand", $message);//This One Not Working
echo $rand[array_rand($rand, 1)]. $modifiedTextAreaText ;//This one working only for the first line

谢谢

你的意思是:

<?php
$randomWords = array('Love', 'My', 'Other');
$randomKey = array_rand($randomWords, 1);
echo $randomWords[$randomKey] . " One<br />";
$randomKey = array_rand($randomWords, 1);
echo $randomWords[$randomKey] . " Two<br />";
$randomKey = array_rand($randomWords, 1);
echo $randomWords[$randomKey] . " Three<br />";

您可以构建数组,然后随机化两个数组或只随机化一个数组。然后将两者结合。

<?php
$rands = array("Love", "My", "Other");
shuffle($rands);
$words = array("One", "Two", "Three");
$new = array_combine($rands, $words);
foreach($new as $key => $val){
    echo "$key $val<br />'n";
}

设置数组中的文本,并使用数组元素键调用rand()函数,然后打印该元素示例

$text = array( 0=> "text0", 1=> "text1", 2=> "text2");
$randomtext = rand (0,2);
echo $text['$randtext'];