Решено
$base64 = function ($out) {
$out[1] = '[base64]'. base64_encode($out[1]) .'[/base64]';
return $out[1];
};
$text = preg_replace_callback('|(\<.*\>(\s?\n?).*\</.*\>)|U', $base64, $text);
$text = preg_replace_callback('|(\<.*\>)|U', $base64, $text);
if (strpos($text, '[base64]') !== false) {
$text = str_replace("[/base64]\r\n", "[/base64]", $text);
}
$text = str_replace("\n\r", "<br />", $text);
$text = preg_replace("|\n|", "$1<br />", $text);
$baseDecode = function ($des) {return base64_decode($des[1]);};
$text = preg_replace_callback('|\[base64\](.*)\[/base64\]|U', $baseDecode, $text);