PHP remove line break or CR LF with no success

2019-06-15 05:07发布

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.

9条回答
SAY GOODBYE
2楼-- · 2019-06-15 05:29

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.

查看更多
做个烂人
3楼-- · 2019-06-15 05:35
function clean($text)
{
    return trim(preg_replace('/\\\\r|\\\\n|\\\\t/i', ' ', $text));
}

Works fine.

查看更多
Ridiculous、
4楼-- · 2019-06-15 05:36

Try to use default trim function with "character_mask".

For example:

$text = trim($text, " \t\n\r\0\x0B"); 

Read the official documentation http://php.net/manual/ru/function.trim.php

查看更多
Bombasti
5楼-- · 2019-06-15 05:36

A method I use is echo str_replace(array('\r\n', '\r', '\n', '\t'), array('\\r\\n', '\\r', '\\n', '\\t'), $text);

What this does is allows you to see what characters are causing the breaks in the text, and replace them appropriately. For example, if you have a "\n" breaking your text, when you use this code, it will then display a "\n" in its place. Example:

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

would become:

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

Naturally, there are a large amount of other breaking characters that could be used, but \r\n, \r, \n and \t are the ones most commonly used.

查看更多
虎瘦雄心在
6楼-- · 2019-06-15 05:40

How about the following

function clean($text)
{
    return trim(preg_replace("/(\s*[\r\n]+\s*|\s+)/", ' ', $text));
}

the first part \s*[\r\n]+\s* will replace any line breaks, it's leading spaces and it's tailing spaces into just one space.

the second part \s+ will shrink spaces into one space.

then trim() removes the leading/tailing space.

查看更多
唯我独甜
7楼-- · 2019-06-15 05:44

Maybe you could try walking the text character by character and call ord() on each, so you could see if these break characters are really \r,\ns?

Recently I got a similar problem with a whitespace which turned out to be a non-breakable space not even inside the ASCII table (ord code 194 or something).

If you are interested my solution was not to try and filter breaks, but to filter EVERYTHING except what was expected to be in text, like so:

$text = preg_replace("/[^ \na-zа-я0-9`~\!@#\$%\^&\*\(\)_\+\-\=\[\]\{\}\\\|;\:'\",\.\/\<\>\?]+/ui", "", $text);
查看更多
登录 后发表回答