php 从字符串中删除新行并替换为一个空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3760816/
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
Remove new lines from string and replace with one empty space
提问by James
$string = "
put returns between paragraphs
for linebreak add 2 spaces at end
";
Want to remove all new lines from string.
想要从字符串中删除所有新行。
I've this regex, it can catch all of them, the problem is I don't know with which function should I use it.
我有这个正则表达式,它可以捕获所有这些,问题是我不知道我应该使用哪个函数。
/\r\n|\r|\n/
$string
should become:
$string
应该变成:
$string = "put returns between paragraphs for linebreak add 2 spaces at end ";
回答by jwueller
You have to be cautious of double line breaks, which would cause double spaces. Use this really efficient regular expression:
你必须小心双换行符,这会导致双空格。使用这个非常有效的正则表达式:
$string = trim(preg_replace('/\s\s+/', ' ', $string));
Multiple spaces and newlines are replaced with a single space.
多个空格和换行符被替换为一个空格。
Edit:As others have pointed out, this solution has issues matching single newlines in between words. This is not present in the example, but one can easily see how that situation could occur. An alternative is to do the following:
编辑:正如其他人指出的那样,此解决方案存在匹配单词之间的单个换行符的问题。这在示例中不存在,但可以很容易地看到这种情况是如何发生的。另一种方法是执行以下操作:
$string = trim(preg_replace('/\s+/', ' ', $string));
回答by matsolof
A few comments on the accepted answer:
对接受的答案的一些评论:
The +
means "1 or more". I don't think you need to repeat \s
. I think you can simply write '/\s+/'
.
的+
意思是“1以上”。我不认为你需要重复\s
。我想你可以简单地写'/\s+/'
。
Also, if you want to remove whitespace first and last in the string, add trim
.
此外,如果要删除字符串中的第一个和最后一个空格,请添加trim
.
With these modifications, the code would be:
通过这些修改,代码将是:
$string = preg_replace('/\s+/', ' ', trim($string));
回答by NullUserException
Just use preg_replace()
只需使用 preg_replace()
$string = preg_replace('~[\r\n]+~', '', $string);
You could get away with str_replace()
on this one, although the code doesn't look as clean:
str_replace()
尽管代码看起来不那么干净,但您可以摆脱这个问题:
$string = str_replace(array("\n", "\r"), '', $string);
See it live on ideone
回答by fabrik
$string = str_replace(array("\n", "\r"), ' ', $string);
回答by RobertPitt
You should use str_replace for its speed and using double quotes with an array
您应该使用 str_replace 来提高速度并在数组中使用双引号
str_replace(array("\r\n","\r"),"",$string);
回答by Peter
I'm not sure if this has any value against the already submitted answers but I can just as well post it.
我不确定这对已经提交的答案是否有任何价值,但我也可以发布它。
// Create an array with the values you want to replace
$searches = array("\r", "\n", "\r\n");
// Replace the line breaks with a space
$string = str_replace($searches, " ", $string);
// Replace multiple spaces with one
$output = preg_replace('!\s+!', ' ', $string);
回答by n00dle
PCRE regex replacements can be done using preg_replace: http://php.net/manual/en/function.preg-replace.php
PCRE 正则表达式替换可以使用 preg_replace 完成:http: //php.net/manual/en/function.preg-replace.php
$new_string = preg_replace("/\r\n|\r|\n/", ' ', $old_string);
Would replace new line or return characters with a space. If you don't want anything to replace them, change the 2nd argument to ''
.
将用空格替换新行或返回字符。如果您不想用任何东西替换它们,请将第二个参数更改为''
.
回答by netom
Use this:
用这个:
replace series of newlines with an empty string:
用空字符串替换一系列换行符:
$string = preg_replace("/[\n\r]+/", "", $string);
or you probably want to replace newlines with a single space:
或者您可能想用一个空格替换换行符:
$string = preg_replace("/[\n\r]+/", " ", $string);
回答by Anton
Escape sequence \R
matches a generic newline
转义序列\R
匹配通用换行符
that is, anything considered a linebreak sequenceby Unicode. This includes all characters matched by
\v
(vertical whitespace), and the multi character sequence\x0D\x0A
...
也就是说,任何被 Unicode视为换行符的序列。这包括由
\v
(垂直空白)匹配的所有字符,以及多字符序列\x0D\x0A
...
$string = preg_replace('/\R+/', " ", $string);
In 8-bit non-UTF-8 mode
\R
is equivalent to the following:(?>\r\n|\n|\x0b|\f|\r|\x85)
... pcre.org
在 8 位非 UTF-8 模式下
\R
等效于以下内容:(?>\r\n|\n|\x0b|\f|\r|\x85)
... pcre.org
回答by Md. Al Amin Bhuiyan
Line breaks in text are generally represented as:
文本中的换行符通常表示为:
\r\n- on a windows computer
\r\n- 在 Windows 计算机上
\r- on an Apple computer
\r- 在苹果电脑上
\n- on Linux
\n- 在 Linux 上
//Removes all 3 types of line breaks
$string = str_replace("\r", "", $string);
$string = str_replace("\n", "", $string);