Alex Rivera | Logout

PHP remove line break or CR LF with no success

Asked 2012-03-04T19:31:03.280
16

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.

Edit
Report

1 Answer

2

How about this:

function clean($text)
{
    $parts = explode(' ', $text);
    foreach ($parts as $key => $value)
        $parts[$key] = preg_replace('/\s/', ' ', $value);
    return implode(' ', $parts);
}

Indeed, if instead of cleaning the JSON file like this, you can use json_encode to create it, you will get rid this problem in a previous step.

answered 2012-03-04T21:20:10.287

Your Answer