SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    UK
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help me write this function?

    Hey guys, being a bit of a novice at PHP I'm struggling with this function.
    I want to choose a random word inside the {} to make a different sentence each time I reload my page. The "|" separates each word

    I don't think its a lot of code but could someone help me or point me in the right direction? I was hoping someone had made similar but cant seem to find any php code on the net.

    Code:
    <?php
    $text = "The {quick|slow|reasonably paced} {brown|green|blue|pink} {fox|goat|rat|camel}
    {jumped|walked|hopped} {over|past|under} the {lazy|tired|boring} {dog|cat|stoat}";
    
    echo Spin($text);
    }
    ?>

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function displayQuickBrownFox(){
        
    $text "The {quick|slow|reasonably paced} {brown|green|blue|pink} {fox|goat|rat|camel} {jumped|walked|hopped} {over|past|under} the {lazy|tired|boring} {dog|cat|stoat}";
        
    $x preg_match_all('/{([a-z\| ]+)}/i',$text,$matches);
        
    $xx $matches[0];
        for(
    $i=0$i<count($matches[0]); $i++){
            
    $y explode('|',preg_replace('/{|}/','',$matches[0][$i]));
            
    $z array_rand($y);
            
    $matches[0][$i] = $y[$z] . ($i == (count($matches[0])-3) ? ' the ' '');
        }
        return 
    'The ' implode(' ',$matches[0]);
    }
    echo 
    displayQuickBrownFox(); 
    sample
    http://www.rnel.net/code-snippets/quick_brown_fox.php

    let me know if you need the explanation

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    UK
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I love you so much! in a manly kinda way lol works a treat

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    UK
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just one thing, the text in question was just example.. the words outside the {} could be anything. How would i change the code to allow any text? I'm just working through the man to see how it works

    Thanks for your time.

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    error_reporting
    (-1);
    ini_set('display_errors'true);

    $text "The {quick|slow|reasonably paced} {brown|green|blue|pink} {fox|goat|rat|camel}
    {jumped|walked|hopped} {over|past|under} the {lazy|tired|boring} {dog|cat|stoat}"
    ;

    function 
    spin($text){
      return 
    preg_replace_callback(
        
    '~{([^}]+)}~',
        
    create_function(
          
    '$match',
          
    '$possible = explode("|", $match[1]); return $possible[array_rand($possible)];'
        
    ),
        
    $text
      
    );
    }

    echo 
    spin($text);

    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a similar question
    What if the sentence is this:

    PHP Code:
    <?php

    $rand_sentence 
    "{Please|Just} make this {cool|awesome|random} test sentence {rotate {quickly|fast} and random|spin and be random}";
    my mobile portal
    ghiris.ro

  7. #7
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    UK
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey guys sorry for the late reply, going try this code out now..

    Thanks a lot for your help


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
  •