Author Topic: select line from a file  (Read 1110 times)

0 Members and 1 Guest are viewing this topic.

Offline superpimpTopic starter

  • Irregular
  • Posts: 31
    • View Profile
select line from a file
« on: March 30, 2006, 01:06:56 PM »
How do I select a line from a file? If I have a file like this:

Code: [Select]
hello
hi
test
etc...

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

I tried this:

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

but that doesnt work...

thanks!

Offline ober

  • Pandas pwn j00
  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,312
  • Gender: Male
  • 404? what!?
    • View Profile
    • Windy Hill Productions
select line from a file
« Reply #1 on: March 30, 2006, 01:47:05 PM »
I think that's mostly correct, but you have to specify a path to the file.
PHP 5 - MySQL 5 - Win Vista 64 - Firefox 3, IE 7
Info: PHP Manual

Quote from CV: After nuclear fallout, there'll be zombies everywhere.  You can't be running from them when you're all fat and shit.  They'll just catch you and eat you and take forever to do it and you'll just have to sit there all that much longer.

Offline superpimpTopic starter

  • Irregular
  • Posts: 31
    • View Profile
select line from a file
« Reply #2 on: March 30, 2006, 02:00:03 PM »
Like file('c:\test\file.txt'); ?

Offline ober

  • Pandas pwn j00
  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,312
  • Gender: Male
  • 404? what!?
    • View Profile
    • Windy Hill Productions
select line from a file
« Reply #3 on: March 30, 2006, 02:02:04 PM »
Correct, but relative to your webserver... not the c drive.
PHP 5 - MySQL 5 - Win Vista 64 - Firefox 3, IE 7
Info: PHP Manual

Quote from CV: After nuclear fallout, there'll be zombies everywhere.  You can't be running from them when you're all fat and shit.  They'll just catch you and eat you and take forever to do it and you'll just have to sit there all that much longer.

Offline superpimpTopic starter

  • Irregular
  • Posts: 31
    • View Profile
select line from a file
« Reply #4 on: March 30, 2006, 02:09:43 PM »
well, the file is in the same path as then .php file, so actually I shouldn't specfy a path...

Offline ober

  • Pandas pwn j00
  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,312
  • Gender: Male
  • 404? what!?
    • View Profile
    • Windy Hill Productions
select line from a file
« Reply #5 on: March 30, 2006, 02:49:50 PM »
Code: [Select]
$file=file('test.txt');
$count=count($file);
$random=rand(0,$count-1);
echo $file[$random];

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

The above works for me.
PHP 5 - MySQL 5 - Win Vista 64 - Firefox 3, IE 7
Info: PHP Manual

Quote from CV: After nuclear fallout, there'll be zombies everywhere.  You can't be running from them when you're all fat and shit.  They'll just catch you and eat you and take forever to do it and you'll just have to sit there all that much longer.

Offline superpimpTopic starter

  • Irregular
  • Posts: 31
    • View Profile
select line from a file
« Reply #6 on: April 01, 2006, 06:26:29 AM »
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: [Select]
$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;

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

edit: THANKS, I found the error :)
« Last Edit: April 01, 2006, 06:37:00 AM by superpimp »

Offline kenrbnsn

  • Guru
  • Freak!
  • *
  • Posts: 9,708
  • Gender: Male
    • View Profile
select line from a file
« Reply #7 on: April 01, 2006, 10:55:39 AM »
You're code looks fine. What's in the file?

Ken

Offline superpimpTopic starter

  • Irregular
  • Posts: 31
    • View Profile
select line from a file
« Reply #8 on: April 01, 2006, 11:05:11 AM »
the problem was I opened with <? instead of <?php
stupid mistake...