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? Link to comment https://forums.phpfreaks.com/topic/25520-explode-help-needed/ 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 Link to comment https://forums.phpfreaks.com/topic/25520-explode-help-needed/#findComment-116459 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] Link to comment https://forums.phpfreaks.com/topic/25520-explode-help-needed/#findComment-117000 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 Link to comment https://forums.phpfreaks.com/topic/25520-explode-help-needed/#findComment-117008 Share on other sites More sharing options...
marcus Posted October 30, 2006 Author Share Posted October 30, 2006 Thanks Ken, works beautifully! Link to comment https://forums.phpfreaks.com/topic/25520-explode-help-needed/#findComment-117011 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.