marcus Posted October 29, 2006 Share Posted October 29, 2006 Ok, I'm having trouble exploding text from my file facts.txt[code]<?$r = rand(1,1);$f = explode("*",facts.txt);echo $f[$r];?>[/code]what am i doing wrong? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 29, 2006 Share Posted October 29, 2006 You have to read the file first. What are you trying to do?Ken Quote Link to comment Share on other sites More sharing options...
marcus Posted October 30, 2006 Author Share Posted October 30, 2006 I am trying to explode text from the file.Would I have to do[code]<?$file = fopen("facts.txt","r");$f = explode("*",$file);$r = rand(1,1);echo $f[$r];?>[/code] Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 30, 2006 Share Posted October 30, 2006 Explode() takes a string as it's second parameter. In your script, the variable $file is a file handle not a string. From the description of what you want to do, it looks like you file contains works seperated by the "*" character and you want to select one at random.Try this:[code]<?php$fc = file_get_contents('facts.txt'); //read the contents of the file into a string$fc_array = explode('*',$fc); // create the arrayecho $fc_array[array_rand($fc_array)]; // use the function array_rand() to get a random index in the $fc_array array.?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
marcus Posted October 30, 2006 Author Share Posted October 30, 2006 Thanks Ken, works beautifully! 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.