PHP remove line break or CR LF with no success

Gino picture Gino · Mar 4, 2012 · Viewed 42.4k times · Source

I did a function to remove line break with php with no success, i tryed all replace code and i still get these line break, i create a json file and i can't read it from jsonp with jquery because of these line break seem to break it all.

function clean($text)
{
$text = trim( preg_replace( '/\s+/', ' ', $text ) );  
$text = preg_replace("/(\r\n|\n|\r|\t)/i", '', $text);
return $text;
}

When i look at the source, there some line break appening in all href, img and br this is a json_encode output example:

<a
href=\"http:\/\/example.com\/out\/content\/\" title=\"link to content website\">

line break afer a. it's hapenig to img src and br

the only way i can remove these break it with

$text = preg_replace("/\s/i", '', $text);

But you understant that there's no space left in all the string and it's not what we want.

Answer

Jirka Kopřiva picture Jirka Kopřiva · Mar 4, 2012

this replace works better for me:

= str_replace (array("\r\n", "\n", "\r"), ' ', $text)