Tero

[+] Spin Random Text

Jan 25th, 2020
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.51 KB | None | 0 0
  1. class spintxt {
  2.     public function process($text) {
  3.         return preg_replace_callback(
  4.             '/\{(((?>[^\{\}]+)|(?R))*)\}/x',
  5.             array($this, 'replace'),
  6.             $text
  7.         );
  8.     }
  9.     public function replace($text) {
  10.         $text = $this->process($text[1]);
  11.         $parts = explode('|', $text);
  12.         return $parts[array_rand($parts)];
  13.     }
  14. }
  15.  
  16. $spintxt = new spintxt();
  17. $string  = '{Hello|Hi}, my name is {Jack|Harry|Lucy|Alex|Mary}';
  18. echo $spintxt->process($string);
Advertisement
Add Comment
Please, Sign In to add comment