Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
Advertisement
Add Comment
Please, Sign In to add comment