-3

I want to combine random strings from three txt files, but I am not sure how to do it. My code doesn't work at all.

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

?>
losmee
  • 19
  • 4

2 Answers2

0

You need to call the functions and return something.

function jedan() {
    $f_contents = file("/ime/ime.txt"); 
    return $f_contents[rand(0, count($f_contents) - 1)];
} 

function dva() {
    $f_contents = file("/prezime/prezime.txt"); 
    return $f_contents[rand(0, count($f_contents) - 1)];
}

function tri() {
    $f_contents = file("/email/email.txt"); 
    return $f_contents[rand(0, count($f_contents) - 1)];
}

$line1 = jedan();
$line2 = dva();
$line3 = tri();


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

Or to make it less "WET":

function RandomLine($url) {
    $f_contents = file($url); 
    return $f_contents[rand(0, count($f_contents) - 1)];
} 

$line1 = RandomLine("/ime/ime.txt");
$line2 = RandomLine("/prezime/prezime.txt");
$line3 = RandomLine("/email/email.txt");


$result = "{$line1}{$line2}{$line3}";
echo $result;
Andreas
  • 23,610
  • 6
  • 30
  • 62
-1

You can simply use file_get_contents() which reads content from files. In below scenario i have created three text files in my wamp64 folder, and reading its string and combining its output by appending.

 <?php

   $file1= file_get_contents('C:\wamp64\xyz.txt');
   $file2= file_get_contents('C:\wamp64\abc.txt');
   $file3= file_get_contents('C:\wamp64\pqr.txt');
   echo $file1.$file2.$file3;

 ?>