02.05 php saved
T0aD
Tags add more
 
Note
T0aD did not leave a note
  1. #! /usr/bin/php -q                                                                                             
  2. <?php
  3.  
  4. $str = "{Please|Just} make this {cool|awesome|random} test sentence {rotate {quickly|fast} and random|spin and \
  5. be random}";
  6.  
  7. for ($i = 0; $i < (isset($argv[1]) ? $argv[1] : 10); $i++) {
  8.   echo "$i: ".strrand($str)."\n";
  9. }
  10.  
  11. function strrand($str)
  12. {
  13.   $final = ''; // what would be returned                                                                       
  14.   $s = array();
  15.   $level = 0;
  16.   $l1 = '';
  17.   $l1len = 0; // used to remove trailing character                                                             
  18.   $l0 = '';
  19.   $randomize = false;
  20.   for ($i = 0; isset($str[$i]); $i++) {
  21.     if ($str[$i] == '|') { // if the separator char is there, set a flag to randomize the string               
  22.       $randomize = true;
  23.     }
  24.     if ($level >= 1) {
  25.       $l1 .= $str[$i];
  26.       $l1len++;
  27.     } elseif ($str[$i] != '{') { // level 0 and != than opening character                                       
  28.       $l0 .= $str[$i];
  29.     }
  30.     switch ($str[$i]) {
  31.     case '{':
  32.       $level++;
  33.       break;
  34.     case '}':
  35.       $level--;
  36.       if ($level == 0) { // we come back to floor level ;)                                                     
  37.     // We remove the last } character                                                                       
  38.     $l1[$l1len-1] = '';
  39.     $l0 .= strrand($l1);
  40.     $l1 = '';
  41.     $l1len = 0;
  42.       }
  43.       break;
  44.     }
  45.   }
  46.   if ($randomize == true) {
  47.     $strt = explode('|', $l0);
  48.     $final = $strt[rand(0, sizeof($strt)-1)];
  49.     $randomize = false; // reset the flag                                                                       
  50.   } else { // otherwise we just add this regular string to the final one                                       
  51.     $final = $l0;
  52.   }
  53.   return $final;
  54. }
  55.  
Parsed in 0.078 seconds, using GeSHi 1.0.7.14

Modify this Paste