<?php
// http://stackoverflow.com/q/33730320/5290909

	$y = 'r';
	$y = preg_quote($y, '/');
	$x = 'M';
	$x = preg_quote($x, '/');
	$subject = 'rrrrrr rrrrr rrrrrr rrrr rrrr.';
	
	$regex = "/\G(?:^|(?(?<!^.).)..(?:.{4})*?)\K$y/s";
	
	$result = preg_replace($regex, $x, $subject);
	
	echo $result;
	// => MrrMrr MrrrM rrMrrr rrrM rrMr.
	
?>