Discover Habbo's history
Treat yourself with a Secret Santa gift.... of a random Wiki page for you to start exploring Habbo's history!
Happy holidays!
Celebrate with us at Habbox on the hotel, on our Forum and right here!
Join Habbox!
One of us! One of us! Click here to see the roles you could take as part of the Habbox community!


Page 1 of 2 12 LastLast
Results 1 to 10 of 12
  1. #1
    Join Date
    Jun 2005
    Location
    /dev/null
    Posts
    4,918
    Tokens
    126

    Latest Awards:

    Default Strings as lines from other strings?

    Edit: The title is confusing because I wrote it with a different idea in mine and forgot to change it :S.


    Basically I have this PHP which takes a text file and outputs a random 8 lines from it. Now this is fine, except sometimes you get repeats which I do not want. I figure it will be complicated to take the code that I have and modify it so that it has no repeats.

    What I want to do is make it shuffle the lines in the text file and then output the top 8 or something.

    What I have:

    PHP Code:
    <?php

                    
    function RandomLine($seed) {
                 
                                 
    $sites file('/dir/to/file.txt');                      
                                 
    $string $sites[array_rand($sites)];              

                    return 
    $string;
                    }
                    
                    
    $s0 RandomLine(rand((float) microtime(), 1000003));
                    
    $s1 RandomLine(rand((float) microtime(), 1000003));
                    
    $s2 RandomLine(rand((float) microtime(), 1000003));
                    
    $s3 RandomLine(rand((float) microtime(), 1000003));
                    
    $s4 RandomLine(rand((float) microtime(), 1000003));
                    
    $s5 RandomLine(rand((float) microtime(), 1000003));
                    
    $s6 RandomLine(rand((float) microtime(), 1000003));
                    
    $s7 RandomLine(rand((float) microtime(), 1000003));

               
    $link = array($s0$s1$s2$s3$s4$s5$s7$s7);

    shuffle($link);

    foreach (
    $link as $value)
    {
        
    $explode explode('|'$value);
        echo 
    '<p>' $explode[2] . ' <a href="' $explode[0] . '">' $explode[1] . '</a> ' $explode[2] . '</p>';
    }

    ?>

    I want to take /dir/to/file.txt, shuffle it and feed the top 8 lines or whatever into my explode.
    Last edited by N!ck; 09-06-2010 at 03:30 PM.

  2. #2
    Join Date
    Nov 2007
    Posts
    1,253
    Tokens
    150

    Latest Awards:

    Default

    I'm embarrassed by this 30 sec code. But something like this should do what you want

    PHP Code:
    <?php

    $file 
    file_get_contents'/dir/to/file.txt' );

    $fileSegments explode'|'$file );
    $fileSegments shuffle$fileSegments );

    for (
    $i 0$i $i++) {
    {
        echo 
    $fileSegments[$i];
    }

    ?>
    Maybe I mis-understood what you want. But that would take a file like:

    This is a sentence.|This is another sentence, yay.|One More

    You could easily change the explode to "\n" or something. Hope it helps.
    Last edited by Source; 09-06-2010 at 03:39 PM.

  3. #3
    Join Date
    Jun 2005
    Location
    /dev/null
    Posts
    4,918
    Tokens
    126

    Latest Awards:

    Default

    Quote Originally Posted by Source View Post
    I'm embarrassed by this 30 sec code. But something like this should do what you want

    PHP Code:
    <?php

    $file 
    file_get_contents'/dir/to/file.txt' );

    $fileSegments explode'|'$file );
    $fileSegments shuffle$fileSegments );

    for (
    $i 0$i $i++) {
    {
        echo 
    $fileSegments[$i];
    }

    ?>
    Maybe I mis-understood what you want. But that would take a file like:

    This is a sentence.|This is another sentence, yay.|One More

    You could easily change the explode to "\n" or something. Hope it helps.
    The file is more complex than that. It looks like:

    Code:
    http://url1.com|words that link to url1
    http://url2.com|words that link to url2
    http://url3.com|words that link to url3
    http://url4.com|words that link to url4
    http://url5.com|words that link to url5
    etc...
    The explode that I have does exactly what I want. Imagine this list has 20+ lines. I want to display a random 8 or so without any repeats.

    What I think your does would shuffle my "http://urln.com|words that link to urln" around. And not look at the lines?
    Last edited by N!ck; 09-06-2010 at 03:48 PM.

  4. #4
    Join Date
    Nov 2007
    Posts
    1,253
    Tokens
    150

    Latest Awards:

    Default

    Well let me edit the script. You should have included the structure of your file in the original post...

    Edit:

    Here:

    PHP Code:
    <?php

    $file 
    file_get_contents'/dir/to/file.txt' );

    $fileLines explode"\n"$file );
    $fileLines shuffle$fileLines );

    for (
    $i 0$i $i++) {
    {
        
    $explode explode'|'$fileLines[$i] );
        
    print_r$explode ); // You can work the rest out yourself
    }

    ?>
    That would take

    Code:
    http://www.google.com|Awesome Search Engine
    http://www.bing.com|Not So Awesome Search Engine
    Randomise the lines, and then explode each line into seperate arrays.
    Last edited by Source; 09-06-2010 at 03:52 PM.

  5. #5
    Join Date
    Jun 2005
    Location
    /dev/null
    Posts
    4,918
    Tokens
    126

    Latest Awards:

    Default

    Quote Originally Posted by Source View Post
    Well let me edit the script. You should have included the structure of your file in the original post...
    Yeah, sorry about that. I just thought I could keep the same explode so didn't think you needed the format.

  6. #6
    Join Date
    Nov 2007
    Posts
    1,253
    Tokens
    150

    Latest Awards:

    Default

    I think my last post should now do what you want.

  7. #7
    Join Date
    Jun 2005
    Location
    /dev/null
    Posts
    4,918
    Tokens
    126

    Latest Awards:

    Default

    +rep'd you.

    Struggling to make this work though. Getting PHP Notice: Undefined offset errors for the "echo" line

    PHP Code:
    <?php

    $file 
    file('/filehere.txt');

    function 
    explodeRows($file) {
      
    $list explode("\n"$file);
      return 
    $list;
    }

    shuffle($list);

    for (
    $i 1$i $i++) {

        
    $explode explode('|'$list);
        echo 
    '<p>' $explode[2] . ' <a href="' $explode[0] . '">' $explode[1] . '</a> ' $explode[2] . '</p>';
    }

    ?>
    Last edited by N!ck; 09-06-2010 at 04:47 PM.

  8. #8
    Join Date
    Nov 2007
    Posts
    1,253
    Tokens
    150

    Latest Awards:

    Default

    Probably because it looks nothing like the code I gave you. If the example file you showed earlier in the thread is correct then this WILL work:

    PHP Code:
    <?php

    $file 
    file_get_contents'/dir/to/file.txt' );

    $fileLines explode"\n"$file );
    shuffle$fileLines );

    for (
    $i 0$i $i++) 
    {
        
    $explode explode'|'$fileLines[$i] );
        echo 
    '<p><a href="' $explode[0] . '">' $explode[1] . '</a></p>';
    }

    ?>
    Ignor the fact I was defining the fileLines with a shuffle which was returning a bool... silly me!

    http://cl.ly/ecfe725cae05c9490963

    And the text file:

    Code:
    http://www.google.com|An awesome search engine
    http://www.bing.com|A meh search engine
    http://www.yahoo.com|A dead search engine
    Last edited by Source; 09-06-2010 at 05:58 PM.

  9. #9
    Join Date
    Jun 2005
    Location
    /dev/null
    Posts
    4,918
    Tokens
    126

    Latest Awards:

    Default

    Quote Originally Posted by Source View Post
    Probably because it looks nothing like the code I gave you. If the example file you showed earlier in the thread is correct then this WILL work:

    PHP Code:
    <?php

    $file 
    file_get_contents'/dir/to/file.txt' );

    $fileLines explode"\n"$file );
    shuffle$fileLines );

    for (
    $i 0$i $i++) 
    {
        
    $explode explode'|'$fileLines[$i] );
        echo 
    '<p><a href="' $explode[0] . '">' $explode[1] . '</a></p>';
    }

    ?>
    Ignor the fact I was defining the fileLines with a shuffle which was returning a bool... silly me!

    http://cl.ly/ecfe725cae05c9490963

    And the text file:

    Code:
    http://www.google.com|An awesome search engine
    http://www.bing.com|A meh search engine
    http://www.yahoo.com|A dead search engine
    Yep, it's working great. Thanks!

  10. #10

    Default

    With all due respect, this is what databases were developed for. Why can't you use one?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •