Results 1 to 8 of 8

Thread: Fetching random lines beginning with a specific word

  1. #1
    Join Date
    May 2009
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Fetching random lines beginning with a specific word

    Hi,

    I was wondering if there was a way to fetch a random line from a txt file that started with a specific word... the txt file looks something like this:

    word1 more words here
    word1 more words here
    word2 more words here
    word2 more words here
    word2 more words here
    word3 more words here
    word3 more words here
    word4 more words here

    And I was hoping for a way to fetch a random line only from those beginning with "word2" for example.

    Any help is much appreciated!

    Thanks,
    Rachel
    Last edited by Snookerman; 05-13-2009 at 06:05 AM. Reason: added “Resolved” prefix

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Try something like this:
    PHP Code:
    <?php
    function randomString ($arr){
        if(!
    is_array($arr)){
            return 
    '$arr not an array.';
        }
        if(
    count($arr) == 0){
            return 
    'None found';
        }
        return 
    $arr[rand(0count($arr)-1)];
    }
    $file fopen("words.txt""rb");
    $fetch "word2";
    $found = array();
    while(
    $line fgetcsv($file100" ")){
        if(
    $line[0] == $fetch){
            
    $found[] = implode($line" ");
        }
    }
    echo 
    randomString($found);
    ?>
    Jeremy | jfein.net

  3. The Following User Says Thank You to Nile For This Useful Post:

    rachelk (05-11-2009)

  4. #3
    Join Date
    May 2009
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Thanks so much for your help, it works perfectly! Would it also be possible to do this with a txt file that looked like this, with no spaces in between words?

    /word1/more-words-here
    /word1/more-words-here
    /word2/more-words-here
    /word2/more-words-here
    /word2/more-words-here
    /word3/more-words-here
    /word3/more-words-here
    /word4/more-words-here

    I tried changing
    Code:
    while($line = fgetcsv($file, 100, " ")){
    to
    Code:
    while($line = fgetcsv($file, 100, "/")){
    but it doesn't seem to work for me.

  5. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    This should do it:
    PHP Code:
    <?php 
    function randomString ($arr){ 
        if(!
    is_array($arr)){ 
            return 
    '$arr not an array.'
        } 
        if(
    count($arr) == 0){ 
            return 
    'None found'
        } 
        return 
    $arr[rand(0count($arr)-1)]; 

    $file fopen("words.txt""rb"); 
    $fetch "word2"
    $found = array(); 
    while(
    $line fgetcsv($file100"/")){ 
        if(
    $line[1] == $fetch){ 
            
    $found[] = implode($line"/"); 
        } 

    echo 
    randomString($found); 
    ?>
    Jeremy | jfein.net

  6. The Following User Says Thank You to Nile For This Useful Post:

    rachelk (05-11-2009)

  7. #5
    Join Date
    May 2009
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Thank you so much for your time Nile, I've just tested it out and it works perfectly!

    I'm planning on having around 10 of these random lines on the same page (each one getting a random line with a different specific word at the start, from the same txt file), would it be ok to have 10 copies of this same piece of coding with the variable names changed for each? like

    #1
    PHP Code:
    <?php 
    function randomString ($arr){ 
        if(!
    is_array($arr)){ 
            return 
    '$arr not an array.'
        } 
        if(
    count($arr) == 0){ 
            return 
    'None found'
        } 
        return 
    $arr[rand(0count($arr)-1)]; 

    $file fopen("words.txt""rb"); 
    $fetch "word2"
    $found = array(); 
    while(
    $line fgetcsv($file100"/")){ 
        if(
    $line[1] == $fetch){ 
            
    $found[] = implode($line"/"); 
        } 

    echo 
    randomString($found); 
    ?>
    #2
    PHP Code:
    <?php 
    function randomString2 ($arr2){ 
        if(!
    is_array($arr2)){ 
            return 
    '$arr2 not an array.'
        } 
        if(
    count($arr2) == 0){ 
            return 
    'None found'
        } 
        return 
    $arr2[rand(0count($arr2)-1)]; 

    $file2 fopen("words.txt""rb"); 
    $fetch2 "word1"
    $found2 = array(); 
    while(
    $line2 fgetcsv($file2100"/")){ 
        if(
    $line2[1] == $fetch2){ 
            
    $found2[] = implode($line2"/"); 
        } 

    echo 
    randomString2($found2); 
    ?>

  8. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    I would do this:
    PHP Code:
    <?php  
    function randomString ($arr){  
        if(!
    is_array($arr)){  
            return 
    '$arr not an array.';  
        }  
        if(
    count($arr) == 0){  
            return 
    'None found';  
        }  
        return 
    $arr[rand(0count($arr)-1)];  
    }
    function 
    randomLine ($file$start){
        
    $file fopen($file"rb");    
        
    $found = array();  
        while(
    $line fgetcsv($file100"/")){  
            if(
    $line[1] == $start){  
                
    $found[] = implode($line"/");  
            }  
        }
        return 
    randomString($found);  
    }
    ?>
    Put that at the top of you're page, and then:
    #1:
    PHP Code:
    echo randomLine("words.txt""word2"); 
    #2:
    PHP Code:
    echo randomLine("words.txt""word1"); 
    And just a little tip, never make a function that has the same actions with a different name, it's pointless.
    Last edited by Nile; 05-12-2009 at 12:18 AM.
    Jeremy | jfein.net

  9. The Following User Says Thank You to Nile For This Useful Post:

    rachelk (05-13-2009)

  10. #7
    Join Date
    May 2009
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Thanks so much Nile

  11. #8
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Anytime.
    Jeremy | jfein.net

Bookmarks

Posting Permissions

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