Tero icon

[+] Spin Random Text

Tero | PRO | 01/25/20 07:07:16 AM UTC | 0 ⭐ | 479 👁️ | Never ⏰ | []
PHP |

510 B

|

None

|

0 👍

/

0 👎

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