Spintext


This is a simple PHP function to generate spintext functionality.

Example

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

Using spintext function output can be

  • This is a Ball
  • This is a Pen
  • That is a Ball
  • That is a Pen.

Spentext 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 "<br/>";
echo $str;