<?php
$text = $_POST['spintax'];

$spintax = new smSpintax();
$comment = $spintax->process($text);

echo $comment;

class smSpintax {
	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)];
	}
}
?>
