superpimp Posted March 30, 2006 Share Posted March 30, 2006 How do I select a line from a file? If I have a file like this:[code]hellohitestetc...[/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! Quote Link to comment Share on other sites More sharing options...
ober Posted March 30, 2006 Share Posted March 30, 2006 I think that's mostly correct, but you have to specify a path to the file. Quote Link to comment Share on other sites More sharing options...
superpimp Posted March 30, 2006 Author Share Posted March 30, 2006 Like file('c:\test\file.txt'); ? Quote Link to comment Share on other sites More sharing options...
ober Posted March 30, 2006 Share Posted March 30, 2006 Correct, but relative to your webserver... not the c drive. Quote Link to comment Share on other sites More sharing options...
superpimp Posted March 30, 2006 Author Share Posted March 30, 2006 well, the file is in the same path as then .php file, so actually I shouldn't specfy a path... Quote Link to comment Share on other sites More sharing options...
ober Posted March 30, 2006 Share Posted March 30, 2006 [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. Quote Link to comment Share on other sites More sharing options...
superpimp Posted April 1, 2006 Author Share Posted April 1, 2006 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 :) Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 1, 2006 Share Posted April 1, 2006 You're code looks fine. What's in the file?Ken Quote Link to comment Share on other sites More sharing options...
superpimp Posted April 1, 2006 Author Share Posted April 1, 2006 the problem was I opened with <? instead of <?phpstupid mistake... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.