tacopalypse
Senior Member
- Nov 30, 2009
- 935
- 2,533
just me having some fun with recursion lol
spinner syntax parser in 9 lines:
use it like this:
causes a stack overflow if the input isn't formatted correctly 
enjoy
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($x, 0, $p1) . $a[array_rand($a)] . substr($x, $p2 + 1), -1);
}
PHP:
$str = "{the {quick|fast} brown fox|lol} {jumps {over|under}|leaps {through|on}} the lazy dog{.|!}";
echo spin($str);
enjoy