Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $text = '{test|test2|test3} some other stuff {some1|some2|some3}';
- $parser = new Parser($text);
- $parser->parse();
- for($i=0;$i<4;$i++)
- echo $parser->replace().'<br />';
- class Parser
- {
- private $vars;
- private $text;
- public function __construct($text)
- {
- $this->text = $text;
- $this->vars = array();
- }
- public function parse()
- {
- $pattern = '/\{([^}]+)\}/';
- $matches = array();
- preg_match_all($pattern, $this->text, $matches);
- $matches = array_shift($matches);
- foreach($matches as $match)
- {
- $this->vars[$match]= explode('|', str_replace(array('{', '}'), '', $match));
- }
- }
- public function replace()
- {
- $text = $this->text;
- foreach($this->vars as $k => $v)
- {
- $rand = rand(0, count($v)-1);
- $text = str_replace($k, $v[$rand], $text);
- }
- return $text;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment