<?php
//Example of user input
$str = "aaaaaaaaaaabbccccccccaaaaad d d d d d d ddde''''''''''''";
function stripRepeat($str) {
//Do not allow repeated whitespace
//$str = preg_replace("/(\s){2,}/",'$1',$str);
//Result: aaaaaaaaaaabbccccccccaaaaad d d d d d d ddde''''''''''''
//Do not allow more than 3 identical characters separated by any whitespace
$str = preg_replace('{( ?.)\1{4,}}','$1$1$1',$str);
//Final result: aaabbcccaaad d d ddde'''