PHP 删除换行符或 CR LF 没有成功

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9558110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:07:24  来源:igfitidea点击:

PHP remove line break or CR LF with no success

phppreg-replaceline-breaks

提问by Gino

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.

我做了一个用 php 删除换行符的函数,但没有成功,我尝试了所有替换代码,但仍然得到这些换行符,我创建了一个 json 文件,但由于这些换行符似乎无法从 jsonp 中使用 jquery 读取它打破这一切。

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:

当我查看源代码时,所有 href、img 和 br 中都有一些换行符,这是一个 json_encode 输出示例:

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

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

换行 afer a. img src 和 br 是hapenig

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.

但是您明白所有字符串中都没有剩余空间,这不是我们想要的。

回答by Jirka Kop?iva

this replace works better for me:

这个替换对我来说效果更好:

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

回答by J. Bruni

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_encodeto create it, you will get rid this problem in a previous step.

确实,如果不是像这样清理 JSON 文件,而是使用json_encode来创建它,您将在上一步中解决这个问题。

回答by Correcter

Try to use default trim function with "character_mask".

尝试使用带有“character_mask”的默认修剪功能。

For example:

例如:

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

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

阅读官方文档http://php.net/manual/ru/function.trim.php

回答by Hyman Ting

How about the following

下面怎么样

function clean($text)
{
    return trim(preg_replace('/\\r|\\n|\\t/i', ' ', $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.

第一部分\s*[\r\n]+\s*将替换任何换行符,它是前导空格并将尾随空格变成一个空格。

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

第二部分\s+将空间缩小为一个空间。

then trim()removes the leading/tailing space.

然后trim()删除前导/尾随空格。

回答by G?khan Yilmaz

$text = str_replace("\r", "", $text);

Works fine.

工作正常。

回答by Apostolos

If you want to remove the CRs and keep the LFs, it's really very simple (just common sense):

如果你想删除 CRs 并保留 LFs,这真的很简单(只是常识):

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

回答by Connor Deckers

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

我使用的方法是 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:

这样做的目的是让您查看导致文本中断的字符,并适当替换它们。例如,如果您有一个“\n”破坏您的文本,当您使用此代码时,它将在其位置显示一个“\n”。例子:

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

would become:

会成为:

$myobj = array( 'foo' => 'bar', 'foz' => 'baz')

$json_myobj = json_encode($myobj);
echo $json_myobj;

$myobj = json_decode($json_myobj);
print_r($myobj);

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.

当然,还有很多其他的断点字符可以使用,但 \r\n、\r、\n 和 \t 是最常用的。

回答by rjz

use json_encode()and json_decode()from the JSON extension to handle JSON de/serialization tasks:

使用JSON 扩展中的json_encode()json_decode()来处理 JSON 反/序列化任务:

$text = preg_replace("/[^ \na-zа-я0-9`~\!@#$%\^&\*\(\)_\+\-\=\[\]\{\}\\|;\:'\",\.\/\<\>\?]+/ui", "", $text);

回答by Tony Bogdanov

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?

也许您可以尝试逐个字符地遍历文本并调用ord()每个字符,这样您就可以查看这些中断字符是否真的\r,\n是 s?

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).

最近我遇到了一个类似的问题,一个空格竟然是一个不可破坏的空间,甚至在 ASCII 表(ord 代码 194 或其他东西)中也没有。

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:

如果您有兴趣,我的解决方案不是尝试过滤中断,而是过滤除文本中预期的内容之外的所有内容,如下所示:

##代码##