<?php
$str =
"
line 1
line 2
line 3\r
line 4
";
$str_cleared = join("\n", array_map('trim', explode("\n", $str)));
var_dump($str);
var_dump($str_cleared);
string(50) "
line 1
line 2
line 3
line 4
"
string(29) "
line 1
line 2
line 3
line 4
"