fork(3) download
  1. <?
  2. function sklonit($text) {
  3. $RE=<<< HEREDOC
  4.   (?:
  5.   \\[
  6.   (\\d++)(?=:)
  7.   |
  8.   (\\d++)
  9.   \\[
  10.   (?!\\d+:)([^:]*?)
  11.   )
  12.  
  13.   :
  14.   ([^\\]\\(]+?)
  15.   \\(
  16.   ([^\\],]*?)
  17.   ,
  18.   ([^\\],]*?)
  19.   ,
  20.   ([^\\],]*?)
  21.   \\)
  22.   \\]
  23. HEREDOC;
  24. $clb=function ($match){
  25. $cases= Array (2, 0, 1, 1, 1, 2);
  26. $titles= Array($match[5], $match[6], $match[7]);
  27. $num=(int) $match[1]?$match[1]:$match[2];
  28. return ($match[1]?"":$match[2].$match[3]).$match[4].$titles[($num%100>4 && $num%100<20)? 2 : $cases[min($num%10, 5)]];
  29. };
  30. return preg_replace_callback("/$RE/xSu", $clb, $text);
  31. };
  32.  
  33. $text= file_get_contents("php://stdin");
  34. echo sklonit($text);
  35. ?>
Success #stdin #stdout 0.01s 20520KB
stdin
У Васи было 5 [5:яблок(о,а,)], а у Тани 11 [11:груш(а,и,)]. И вот же дилема- как эти [2:богатств(о,а,)] поделить на 105 [105:негрит(енок,нка,ят)] ?
stdout
У Васи было 5 яблок, а у Тани 11 груш. И вот же дилема- как эти богатства поделить на 105 негритят ?