Jump to content

URGENT! Random no of lines from a text File... :-(


natasha_thomas

Recommended Posts

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.  :o

 

Can you suggest me some other Codes? Or will this code work?

 

Any Expert can help?

Natty Thomas

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

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;
}

 

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.

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.....

 

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;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.