组合来自多个txt文件的随机字符串并回显

我想组合来自三个 txt 文件的随机字符串,但我不知道该怎么做。我的代码根本不起作用。


<?php


function jedan() {

    $f_contents = file("/ime/ime.txt"); 

    $line1 = $f_contents[rand(0, count($f_contents) - 1)];


    function dva() {

    $f_contents = file("/prezime/prezime.txt"); 

    $line2 = $f_contents[rand(0, count($f_contents) - 1)];

    }


    function tri() {

    $f_contents = file("/email/email.txt"); 

    $line3 = $f_contents[rand(0, count($f_contents) - 1)];

    }



    $result = "{$line1}{$line2}{$line3}";

    echo $result


?>


牛魔王的故事
浏览 110回答 2
2回答

斯蒂芬大帝

你需要调用函数并返回一些东西。function jedan() {&nbsp; &nbsp; $f_contents = file("/ime/ime.txt");&nbsp;&nbsp; &nbsp; return $f_contents[rand(0, count($f_contents) - 1)];}&nbsp;function dva() {&nbsp; &nbsp; $f_contents = file("/prezime/prezime.txt");&nbsp;&nbsp; &nbsp; return $f_contents[rand(0, count($f_contents) - 1)];}function tri() {&nbsp; &nbsp; $f_contents = file("/email/email.txt");&nbsp;&nbsp; &nbsp; return $f_contents[rand(0, count($f_contents) - 1)];}$line1 = jedan();$line2 = dva();$line3 = tri();$result = "{$line1}{$line2}{$line3}";echo $result;或者让它不那么“湿”:function RandomLine($url) {&nbsp; &nbsp; $f_contents = file($url);&nbsp;&nbsp; &nbsp; return $f_contents[rand(0, count($f_contents) - 1)];}&nbsp;$line1 = RandomLine("/ime/ime.txt");$line2 = RandomLine("/prezime/prezime.txt");$line3 = RandomLine("/email/email.txt");$result = "{$line1}{$line2}{$line3}";echo $result;

忽然笑

您可以简单地使用 file_get_contents() 从文件中读取内容。在下面的场景中,我在 wamp64 文件夹中创建了三个文本文件,并读取其字符串并通过附加组合其输出。&nbsp;<?php&nbsp; &nbsp;$file1= file_get_contents('C:\wamp64\xyz.txt');&nbsp; &nbsp;$file2= file_get_contents('C:\wamp64\abc.txt');&nbsp; &nbsp;$file3= file_get_contents('C:\wamp64\pqr.txt');&nbsp; &nbsp;echo $file1.$file2.$file3;&nbsp;?>
打开App,查看更多内容
随时随地看视频慕课网APP