Advertisement
aivavic

test_10

Jan 13th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.62 KB | None | 0 0
  1. class testWork
  2. {
  3. public function task10($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, count($result)-1)];
  19.     }
  20. }
  21. $str = '{Я помню|Не помню|Какое} чудное {мгновенье|затменье|творенье}';
  22. $obj = new testWork();
  23. $obj->task10($str);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement