02.05
php
saved
T0aD
Note
T0aD did not leave a note
T0aD did not leave a note
- #! /usr/bin/php -q
- <?php
- $str = "{Please|Just} make this {cool|awesome|random} test sentence {rotate {quickly|fast} and random|spin and \
- be random}";
- }
- function strrand($str)
- {
- $final = ''; // what would be returned
- $level = 0;
- $l1 = '';
- $l1len = 0; // used to remove trailing character
- $l0 = '';
- $randomize = false;
- if ($str[$i] == '|') { // if the separator char is there, set a flag to randomize the string
- $randomize = true;
- }
- if ($level >= 1) {
- $l1 .= $str[$i];
- $l1len++;
- } elseif ($str[$i] != '{') { // level 0 and != than opening character
- $l0 .= $str[$i];
- }
- switch ($str[$i]) {
- case '{':
- $level++;
- break;
- case '}':
- $level--;
- if ($level == 0) { // we come back to floor level ;)
- // We remove the last } character
- $l1[$l1len-1] = '';
- $l0 .= strrand($l1);
- $l1 = '';
- $l1len = 0;
- }
- break;
- }
- }
- if ($randomize == true) {
- $randomize = false; // reset the flag
- } else { // otherwise we just add this regular string to the final one
- $final = $l0;
- }
- return $final;
- }
Parsed in 0.078 seconds, using GeSHi 1.0.7.14