class spintxt {
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)];
}
}
$spintxt = new spintxt();
$string = '{Hello|Hi}, my name is {Jack|Harry|Lucy|Alex|Mary}';
echo $spintxt->process($string);
Comments