Need some help - pick random lines from text files & email
Moderator: General Moderators
-
StudioWorks
- Forum Newbie
- Posts: 7
- Joined: Wed Mar 08, 2017 9:34 am
Need some help - pick random lines from text files & email
Hello everybody,
I am kind of new to this so I am hoping that you can give me a hand. The task is this:
I have 5 .txt files, each file contains 10 lines of text. I have to randomly select one line from each file and the these generated lines have to be sent through email to two email addresses.
Can you please provide some pointers ?
Thank you!
I am kind of new to this so I am hoping that you can give me a hand. The task is this:
I have 5 .txt files, each file contains 10 lines of text. I have to randomly select one line from each file and the these generated lines have to be sent through email to two email addresses.
Can you please provide some pointers ?
Thank you!
Re: Need some help - pick random lines from text files & ema
What have you done so far and where are you encountering issues?
-
StudioWorks
- Forum Newbie
- Posts: 7
- Joined: Wed Mar 08, 2017 9:34 am
Re: Need some help - pick random lines from text files & ema
So fa I can select random lines from one .txt files however I am having troubles selecting from multiple .txt files, don't know exactly how to do it.
Re: Need some help - pick random lines from text files & ema
How are you selecting from the one file? How are you storing the results? Do you have a list of files you could select from?
-
StudioWorks
- Forum Newbie
- Posts: 7
- Joined: Wed Mar 08, 2017 9:34 am
Re: Need some help - pick random lines from text files & ema
So far only have this to select a random line from just one file:
<?php
$content = file("file.txt");
$line = $content[rand(0, count($content) - 1)];
?>
As i said i don't know how to handle this with multiple files or how to send the result through email..
<?php
$content = file("file.txt");
$line = $content[rand(0, count($content) - 1)];
?>
As i said i don't know how to handle this with multiple files or how to send the result through email..
Re: Need some help - pick random lines from text files & ema
What if you used an array to store the list of filenames and another array to store the randomly selected lines?
-
StudioWorks
- Forum Newbie
- Posts: 7
- Joined: Wed Mar 08, 2017 9:34 am
Re: Need some help - pick random lines from text files & ema
Hey,
And how would I do that ? As i said, i'm new to this. I'm not expecting something for free, I can pay for your help if you want.
And how would I do that ? As i said, i'm new to this. I'm not expecting something for free, I can pay for your help if you want.
Re: Need some help - pick random lines from text files & ema
I'm not looking for payment. I'm trying to help you arrive at the answer yourself rather than doing your homework for you.
-
StudioWorks
- Forum Newbie
- Posts: 7
- Joined: Wed Mar 08, 2017 9:34 am
Re: Need some help - pick random lines from text files & ema
Hey, thank you for your help! i would gladly stay and study this, it's not homework it's a favor for a friend and this is not exactly my area and it needs to be done by tomorrow, that's why the desperation 
Re: Need some help - pick random lines from text files & ema
Instead of hardcoding "file.txt" into your file() call, create an array of all the files and iterate over it. Create a separate array for the lines you retrieve and push new items into the array with each iteration.
Something like this should give you an idea
Something like this should give you an idea
Code: Select all
$files = ['file.txt', 'file2.txt', 'file3.txt'];
$lines = [];
foreach ($files as $file) {
$contents = file($file);
array_shuffle($contents);
$lines[] = array_pop($content);
}-
StudioWorks
- Forum Newbie
- Posts: 7
- Joined: Wed Mar 08, 2017 9:34 am
Re: Need some help - pick random lines from text files & ema
I will give this a try and let you know how it goes!! Thank you so much for your help and patience!! ( I wasn't joking when I said that i don't expect things for free! )
-
StudioWorks
- Forum Newbie
- Posts: 7
- Joined: Wed Mar 08, 2017 9:34 am
Re: Need some help - pick random lines from text files & ema
Thank you, you've been very helpful. I started out from your advice and I managed to get the job done.
Thank you again!
Thank you again!