Jump to content

Archived

This topic is now archived and is closed to further replies.

superpimp

select line from a file

Recommended Posts

How do I select a line from a file? If I have a file like this:

[code]
hello
hi
test
etc...
[/code]

I want to select like "hello" and "hi", ... as seperate text.

I tried this:

[code]
<?php
$file=file('file.txt');
$count=count($file);
$random=rand(0,$count);
echo $file[$random];
?>
[/code]

but that doesnt work...

thx!

Share this post


Link to post
Share on other sites
I think that's mostly correct, but you have to specify a path to the file.

Share this post


Link to post
Share on other sites
Correct, but relative to your webserver... not the c drive.

Share this post


Link to post
Share on other sites
[code]$file=file('test.txt');
$count=count($file);
$random=rand(0,$count-1);
echo $file[$random];[/code]

Color me wrong... you don't have to specify a path.

The above works for me.

Share this post


Link to post
Share on other sites
well, it sucks. that doesnt work. its very weird, i've added extra things, that work in other scripts but they wont work here, grrr. I even tried your code, doenst work either :(

[code]
$file=file('test.txt');
$file2=file_get_contents("test.txt");
$filecontents = file_get_contents("test.txt");
$count=count($file);
$random=rand(0,$count-1);
echo $file[$random];
echo $file2;
echo $filecontents;
[/code]

this gives a blank screen :( What am I doing wrong???

edit: THANKS, I found the error :)

Share this post


Link to post
Share on other sites
You're code looks fine. What's in the file?

Ken

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.