1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[GET] PHP Spinner Syntax Parser Script

Discussion in 'PHP & Perl' started by tacopalypse, Jul 6, 2010.

  1. tacopalypse

    tacopalypse Executive VIP Jr. VIP Premium Member

    Joined:
    Nov 30, 2009
    Messages:
    980
    Likes Received:
    2,485
    Home Page:
    just me having some fun with recursion lol
    spinner syntax parser in 9 lines:
    PHP:
    function spin($x$p1 = -1){
        if(
    $p1 === false)
            return 
    $x;
        
    $p2 strpos($x"}"$p1 1);
        
    $p3 strpos($x"{"$p1 1);
        if(
    $p2 $p3 && $p3 !== false || $p1 == -1)
            return 
    spin($x$p3);
        
    $a explode("|"substr($x$p1 1$p2 $p1 1));
        return 
    spin(substr($x0$p1) . $a[array_rand($a)] . substr($x$p2 1), -1);
    }
    use it like this:
    PHP:
    $str "{the {quick|fast} brown fox|lol} {jumps {over|under}|leaps {through|on}} the lazy dog{.|!}";
    echo 
    spin($str);
    causes a stack overflow if the input isn't formatted correctly :p

    enjoy :)
     
    • Thanks Thanks x 4
  2. goawayplease

    goawayplease Regular Member

    Joined:
    Apr 10, 2008
    Messages:
    299
    Likes Received:
    67
    haha jeez that is some efficient looking code.