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! Link to comment https://forums.phpfreaks.com/topic/6192-select-line-from-a-file/ 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. Link to comment https://forums.phpfreaks.com/topic/6192-select-line-from-a-file/#findComment-22373 Share on other sites More sharing options...
superpimp Posted March 30, 2006 Author Share Posted March 30, 2006 Like file('c:\test\file.txt'); ? Link to comment https://forums.phpfreaks.com/topic/6192-select-line-from-a-file/#findComment-22381 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. Link to comment https://forums.phpfreaks.com/topic/6192-select-line-from-a-file/#findComment-22383 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... Link to comment https://forums.phpfreaks.com/topic/6192-select-line-from-a-file/#findComment-22388 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. Link to comment https://forums.phpfreaks.com/topic/6192-select-line-from-a-file/#findComment-22404 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 :) Link to comment https://forums.phpfreaks.com/topic/6192-select-line-from-a-file/#findComment-22858 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 Link to comment https://forums.phpfreaks.com/topic/6192-select-line-from-a-file/#findComment-22893 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... Link to comment https://forums.phpfreaks.com/topic/6192-select-line-from-a-file/#findComment-22895 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.