3v4l.org

run code in 300+ PHP versions simultaneously
<?PHP /** * Spintax - A helper class to process Spintax strings. * @name Spintax * @author Jason Davis - http://www.codedevelopr.com/ */ class Spintax { public function process($text) { return preg_replace_callback( '/\{(((?>[^\{\}]+)|(?R))*)\}/x', array($this, 'replace'), $text ); } public function replace($text) { $text = $this->process($text[1]); $parts = explode('|', $text); return $parts[array_rand($parts)]; } } ?> <?PHP $spintax = new Spintax(); $string = '{Hello|Howdy|Hola} to you, {Mr.|Mrs.|Ms.} {Smith|Williams|Davis}!'; echo $spintax->process($string); ?>

preferences:
60.45 ms | 406 KiB | 5 Q