Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 文件中每行前面的随机字_Php_File_Random - Fatal编程技术网

Php 文件中每行前面的随机字

Php 文件中每行前面的随机字,php,file,random,Php,File,Random,我想在每行前面添加一个随机文本,如下所示: <?php $randomWords = array('Love', 'My', 'Other'); $randomKey = array_rand($randomWords, 1); echo $randomWords[$randomKey] . " One<br />"; $randomKey = array_rand($randomWords, 1); echo $randomWords[$randomKey] . " T

我想在每行前面添加一个随机文本,如下所示:

<?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 />";
之前:

One
Two
Three
之后:

Love One
My Two
Other Three
更新: 很抱歉造成混淆,我的意思是“before”文本是一个textarea提交的文本,因此它在$\u POST值中,我希望结果像“after”文本一样。简单来说,代码如下所示: 假设before文本低于$_POST['message']值,我想回显该值,但前面有随机文本

我尝试了这一点,但只适用于第一行,而不适用于另一行:

$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

谢谢

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


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


你的意思是这样的:

<?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

$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";
}

您可以构建数组,然后将两个数组或仅一个数组混洗。然后将两者结合起来

<?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";
}

这篇文章是从哪里来的?我不明白你的问题。什么样的随机?您可以从数组中获取随机文本,也可以从自动生成的文本中获取随机文本?那么您需要什么类型?这些是可打印的字符串吗?欢迎使用Stackoverflow。在发布任何问题之前,请遵循您需要确认的步骤。请记住,只有你想要的东西,你问自己它是如何编程的,这本身不符合编程问题的条件。你从哪里获取这篇文章?我不理解你的问题。什么样的随机?您可以从数组中获取随机文本,也可以从自动生成的文本中获取随机文本?那么您需要什么类型?这些是可打印的字符串吗?欢迎使用Stackoverflow。在发布任何问题之前,请遵循您需要确认的步骤。请记住,只有你想要某样东西,并且你问自己它是如何编程的,这本身不符合编程问题的要求。你有array_rand()来处理这些东西。你有array_rand()来处理这些东西。