fork download
  1. <?php
  2. // http://stackoverflow.com/q/33730320/5290909
  3.  
  4. $y = 'r';
  5. $y = preg_quote($y, '/');
  6. $x = 'M';
  7. $x = preg_quote($x, '/');
  8. $subject = 'rrrrrr rrrrr rrrrrr rrrr rrrr.';
  9.  
  10. $regex = "/\G(?:^|(?(?<!^.).)..(?:.{4})*?)\K$y/s";
  11.  
  12. $result = preg_replace($regex, $x, $subject);
  13.  
  14. echo $result;
  15. // => MrrMrr MrrrM rrMrrr rrrM rrMr.
  16.  
  17. ?>
Success #stdin #stdout 0.02s 52432KB
stdin
Standard input is empty
stdout
MrrMrr MrrrM rrMrrr rrrM rrMr.