natasha_thomas Posted January 16, 2010 Share Posted January 16, 2010 Friends, Too frustrating... Spent whole day, could not figure out what the heck is going wrong... Requirement: In my wordpress blog, i want to pull Random no of lines (can be 2,3,4,5 uptill total no of lines) from a Text file (hosted on Server), The delimiter should be Period ".". How can i achieve this? I did some crude coding... shame its not working.... function getmyfile() { $file_handle = fopen("/home/powerabc/public_html/demo/demo.txt", "r"); while (!feof($file_handle)) { $userfile = fgets($file_handle); $line = explode(".",$userfile); } echo $line[0]; $rand=rand(1,10); for ( $countr=0; $countr < $rand; $countr+=1){ $valu = $valu . $line[rand(0,5)]; echo $valu; } return $valu; fclose($file_handle); } After this i called this function in a Widget in Wordpress... Its not working. Can you suggest me some other Codes? Or will this code work? Any Expert can help? Natty Thomas Quote Link to comment https://forums.phpfreaks.com/topic/188668-urgent-random-no-of-lines-from-a-text-file/ Share on other sites More sharing options...
jskywalker Posted January 16, 2010 Share Posted January 16, 2010 In my wordpress blog, i want to pull Random no of lines (can be 2,3,4,5 uptill total no of lines) from a Text file (hosted on Server), The delimiter should be Period ".". How can i achieve this? function getmyfile() { $file_handle = fopen("/home/powerabc/public_html/demo/demo.txt", "r"); while (!feof($file_handle)) { $userfile = fgets($file_handle); $line = explode(".",$userfile); } After this code EVERY line is read, and only last line values are in $line echo $line[0];above line should echo the first part of the last line of your file..... $rand=rand(1,10); for ( $countr=0; $countr < $rand; $countr+=1){ $valu = $valu . $line[rand(0,5)]; echo $valu; } return $valu; fclose($file_handle); } Natty Thomas Quote Link to comment https://forums.phpfreaks.com/topic/188668-urgent-random-no-of-lines-from-a-text-file/#findComment-996049 Share on other sites More sharing options...
natasha_thomas Posted January 16, 2010 Author Share Posted January 16, 2010 But this code is not working.... Any change in this code required? Quote Link to comment https://forums.phpfreaks.com/topic/188668-urgent-random-no-of-lines-from-a-text-file/#findComment-996051 Share on other sites More sharing options...
trq Posted January 16, 2010 Share Posted January 16, 2010 If its so urgent maybe you should read the reply properly. Quote Link to comment https://forums.phpfreaks.com/topic/188668-urgent-random-no-of-lines-from-a-text-file/#findComment-996052 Share on other sites More sharing options...
Felex Posted January 16, 2010 Share Posted January 16, 2010 firstly you shall not assign the readed line to $line variable, you shall use $line[], so you can have a array variable with incremented number order as keys, and some more addons... function getmyfile() { $file_handle = fopen("/home/powerabc/public_html/demo/demo.txt", "r"); while (!feof($file_handle)) { $userfile = fgets($file_handle); $line[] = explode(".",$userfile); // $line must be array } fclose($file_handle); // no need to be opened that much time echo $line[0]; $rand=rand(1, count($line)); // so you will have a random number between start and end of your array $valu = ''; // actually if you initialize your variables, you get more error free code for ( $countr=0; $countr < $rand; $countr+=1){ $valu = $valu . $line[$countr]; // this makes more sense to me, cause you can get same lines echo $valu; } return $valu; } Quote Link to comment https://forums.phpfreaks.com/topic/188668-urgent-random-no-of-lines-from-a-text-file/#findComment-996066 Share on other sites More sharing options...
natasha_thomas Posted January 16, 2010 Author Share Posted January 16, 2010 firstly you shall not assign the readed line to $line variable, you shall use $line[], so you can have a array variable with incremented number order as keys, and some more addons... function getmyfile() { $file_handle = fopen("/home/powerabc/public_html/demo/demo.txt", "r"); while (!feof($file_handle)) { $userfile = fgets($file_handle); $line[] = explode(".",$userfile); // $line must be array } fclose($file_handle); // no need to be opened that much time echo $line[0]; $rand=rand(1, count($line)); // so you will have a random number between start and end of your array $valu = ''; // actually if you initialize your variables, you get more error free code for ( $countr=0; $countr < $rand; $countr+=1){ $valu = $valu . $line[$countr]; // this makes more sense to me, cause you can get same lines echo $valu; } return $valu; } Thanks felex, It looks very clean coding. Well Done.. But... when i run this code, i get out put as: ArrayArrayArrayArrayArray am i not supposed to get the content of my text file with this coding? Natasha T. Quote Link to comment https://forums.phpfreaks.com/topic/188668-urgent-random-no-of-lines-from-a-text-file/#findComment-996074 Share on other sites More sharing options...
salathe Posted January 16, 2010 Share Posted January 16, 2010 Do you want to get the first x lines in the file, where x is a random number? Or do you want to get x random lines from anywhere in the file? Quote Link to comment https://forums.phpfreaks.com/topic/188668-urgent-random-no-of-lines-from-a-text-file/#findComment-996080 Share on other sites More sharing options...
jskywalker Posted January 16, 2010 Share Posted January 16, 2010 Code is wrong... 1. while (!feof($file_handle)) 2. { 3. $userfile = fgets($file_handle); 4. $line[] = explode(".",$userfile); // $line must be array } line 4 should read: $line = explode(".", $userfile); // see: http://nl3.php.net/manual/en/function.explode.php when the loop is executed more than once, the previous contents of the array "$line" are LOST..... Quote Link to comment https://forums.phpfreaks.com/topic/188668-urgent-random-no-of-lines-from-a-text-file/#findComment-996081 Share on other sites More sharing options...
natasha_thomas Posted January 16, 2010 Author Share Posted January 16, 2010 Do you want to get the first x lines in the file, where x is a random number? Or do you want to get x random lines from anywhere in the file? I want to get x random lines from anywhere in the file.... Quote Link to comment https://forums.phpfreaks.com/topic/188668-urgent-random-no-of-lines-from-a-text-file/#findComment-996083 Share on other sites More sharing options...
salathe Posted January 16, 2010 Share Posted January 16, 2010 Here's one way, the comments should guide you through what is happening but do feel free to ask about any particular things that might be unclear. // Get all lines from the file into an array // No newline character at end of each line, skip empty lines in the file $file = file('./demo.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); // Get a random number (of lines to show, minimum 2) $num_lines = mt_rand(2, count($file)); // Get keys (line numbers) for $num_lines lines $numbers = array_rand($file, $num_lines); // As of PHP 5.2.10 $numbers will be sequential which we don't want, so shuffle shuffle($numbers); // Output something useful echo "Displaying $num_lines random lines" . PHP_EOL; foreach ($numbers as $number) { echo $file[$number] . PHP_EOL; } Quote Link to comment https://forums.phpfreaks.com/topic/188668-urgent-random-no-of-lines-from-a-text-file/#findComment-996090 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.