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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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