$t = preg_replace_callback( '#(.*?)#is', function ($matches) { $t = str_replace(array('',''),'',$matches[0]); $t = preg_replace_callback( '#(\$\$.*?\$\$)#is', function ($matches) { return '£'.base64_encode($matches[0]).'£'; }, $t ); $t = preg_replace_callback( '#(\$.*?\$)#is', function ($matches) { return '£'.base64_encode($matches[0]).'£'; }, $t ); return $t; }, $t );