Advertisement
aivavic

task_10

Jan 13th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.61 KB | None | 0 0
  1. class testWork
  2. {
  3. public function task_10($str)
  4.     {
  5.         if (!is_string($str))
  6.             throw new InvalidArgumentException('Argument must be String. Input was: ' . $str);
  7.  
  8.         echo preg_replace_callback(
  9.             "#\{(.+?)\}#is",
  10.             [$this, 'callback'],
  11.             $str);
  12.     }
  13.  
  14.     public function callback($matches)
  15.     {
  16.         $result = explode('|', $matches[1]);
  17.  
  18.         return $result[rand(0,2)];
  19.     }
  20. }
  21.  
  22. $str = '{Я помню|Не помню|Какое} чудное {мгновенье|затменье|творенье}';
  23. $obj = new testWork();
  24. $obj->task_10($str);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement