Spintext in php

This is a simple PHP function to generate spintext functionality.

Example

$str = {This|That} is a {Ball|Pen}

Using spintext function output can be

Spintext Function


$str = "{This|That} is a {Ball|Pen}!";
function spintext ($str) {
    return preg_replace_callback("/\{(((?>[^\{\}]+)|(?R))*?)\}/x", function ($text) {
        $words = explode("|", $text[1]);
        return $words[array_rand($words)];
    }, $str);
}
$str = spintext($str);
echo $str;