php 如何从字符串中删除换行符(无字符!)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10757671/
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
How to remove line breaks (no characters!) from the string?
提问by Johannes Pille
This might appear to be a dupe, but rest assured it isn't - I have searched both SO as well as the rest of the web for an answer to my problem and ended up finding the same insufficient "solutions" over and over. Anyhow, here it goes:
这可能看起来是一个骗局,但请放心,它不是 - 我已经搜索了 SO 以及网络的其余部分来寻找我的问题的答案,并最终一遍又一遍地找到相同的不足的“解决方案”。无论如何,它是这样的:
I'm saving user input from a textarea to a MySQL database (within a WordPress environment, but that ought not to matter to this problem, I believe). It is later retrieved from the DB to be shown to Admins in the backend of the site. The problem occurs when users submit text with line breaks (i.e. hit the Enter key).
我正在将用户输入从 textarea 保存到 MySQL 数据库(在 WordPress 环境中,但我相信这与这个问题无关)。稍后从数据库中检索它以显示给站点后端的管理员。当用户提交带有换行符的文本(即按 Enter 键)时,就会出现问题。
A sample string might look like this:
示例字符串可能如下所示:
Dear friends, I just wanted so Hello. How are you guys? I'm fine, thanks!
Greetings,
Bill
There are no end of line characters ("\n", "\r", or the like) in the string.
字符串中没有行尾字符(“\n”、“\r”等)。
I am using nl2br()on it to generate HTML output, but that's not enough. The result then is:
我正在使用nl2br()它来生成 HTML 输出,但这还不够。结果是:
Dear friends, I just wanted so Hello. How are you guys? I'm fine, thanks!<br />
<br />
Greetings,<br />
Bill
Which, as far as I understand it, is the expected nl2br()result, as that inserts the tags and isn't supposed to replace the line-breaks in the first place?
据我了解,哪个是预期的nl2br()结果,因为它会插入标签而不应该首先替换换行符?
However the format I need would be this:
但是我需要的格式是这样的:
Dear friends, I just wanted so Hello. How are you guys? I'm fine, thanks!<br /><br />Greetings,<br />Bill
If the string had EOL characters such as "\n" in it, I'd hit it with either str_replace()or preg_replace()and be done with it, but I have no clue what needle to feed either of those functions if there ain't no characters there in the first place.
如果字符串中包含 EOL 字符,例如 "\n",我会用str_replace()或击中它preg_replace()并完成它,但是如果那里没有字符,我不知道用什么针来馈送这两个函数中的任何一个首先。
I can manually access the relevant field in the DB, hit Backspace for every linebreak and what I later on want to do with the string works. So I know I need the above format.
我可以手动访问数据库中的相关字段,为每个换行符点击 Backspace 以及我稍后想要对字符串执行的操作。所以我知道我需要上述格式。
回答by Demis Palma ツ
Ben's solutionis acceptable, but str_replace()is by far faster than preg_replace()
Ben 的解决方案是可以接受的,但str_replace()比preg_replace()快得多
$buffer = str_replace(array("\r", "\n"), '', $buffer);
Using less CPU power, reduces the world carbon dioxide emissions.
使用更少的 CPU 功率,减少世界二氧化碳排放量。
回答by Ben Roux
You should be able to replace it with a preg that removes all newlines and carriage returns. The code is:
您应该能够用删除所有换行符和回车符的 preg 替换它。代码是:
preg_replace( "/\r|\n/", "", $yourString );
Even though the \ncharacters are not appearing, if you are getting carriage returns there is an invisible character there. The preg replace should grab and fix those.
即使\n字符没有出现,如果你得到回车,那里有一个不可见的字符。preg 替换应该抓住并修复这些。
回答by flowfree
$str = "
Dear friends, I just wanted so Hello. How are you guys? I'm fine, thanks!<br />
<br />
Greetings,<br />
Bill";
echo str_replace(array("\n", "\r"), '', $str); // echo $str in a single line
回答by kenorb
It's because nl2br()doesn't remove new lines at all.
这是因为nl2br()根本不删除新行。
Returns string with
<br />or<br>inserted beforeall newlines (\r\n,\n\r,\nand\r).
返回字符串
<br />或<br>插入之前所有换行符(\r\n,\n\r,\n和\r)。
Use str_replaceinstead:
使用str_replace来代替:
$string = str_replace(array("\r\n", "\r", "\n"), "<br />", $string);
回答by Gijs de Jong
You can also use PHP trim
您也可以使用PHP 修剪
This function returns a string with whitespace stripped from the beginning and end of str. Without the second parameter, trim() will strip these characters:
- " " (ASCII 32 (0x20)), an ordinary space.
- "\t" (ASCII 9 (0x09)), a tab.
- "\n" (ASCII 10 (0x0A)), a new line (line feed).
- "\r" (ASCII 13 (0x0D)), a carriage return.
- "\0" (ASCII 0 (0x00)), the NUL-byte.
- "\x0B" (ASCII 11 (0x0B)), a vertical tab.
此函数返回一个字符串,其中从 str 的开头和结尾去除了空格。如果没有第二个参数,trim() 将去除这些字符:
- " " (ASCII 32 (0x20)),一个普通的空格。
- "\t" (ASCII 9 (0x09)),一个制表符。
- "\n" (ASCII 10 (0x0A)),换行(换行)。
- "\r" (ASCII 13 (0x0D)),回车。
- "\0" (ASCII 0 (0x00)),NUL 字节。
- "\x0B" (ASCII 11 (0x0B)),一个垂直制表符。
回答by Rafanake
str_replace(PHP_EOL, null, $str);
str_replace(PHP_EOL, null, $str);
回答by Ajjaah
To work properly also on Windows I'd suggest to use
为了在 Windows 上也能正常工作,我建议使用
$buffer = str_replace(array("\r\n", "\r", "\n"), "", $buffer);
"\r\n"- for Windows, "\r"- for Mac and "\n"- for Linux
"\r\n"- 适用于 Windows,"\r"- 适用于 Mac 和"\n"- 适用于 Linux
回答by tfont
Something a bit more functional (easy to use anywhere):
功能更强大的东西(易于在任何地方使用):
function strip_carriage_returns($string)
{
return str_replace(array("\n\r", "\n", "\r"), '', $string);
}
Using PHP_EOLas the search replacement parameter is also a good idea! Kudos.
使用PHP_EOL作为搜索替换参数也是一个好主意!荣誉。
回答by NVRM
Alternative built-in: trim()
替代内置:trim()
trim — Strip whitespace (or other characters) from the beginning and end of a string
Description ?
trim ( string $str [, string $character_mask = " \t\n\r$s=str_replace(chr(10),'',$s);
$s=str_replace(chr(13),'',$s);
$s=str_replace("\r\n"),'',$s);
\x0B" ] ) : string
This function returns a string with whitespace stripped from the beginning and end of str.
Without the second parameter, trim() will strip these characters:
" " (ASCII 32 (0x20)), an ordinary space.
"\t" (ASCII 9 (0x09)), a tab.
"\n" (ASCII 10 (0x0A)), a new line (line feed).
"\r" (ASCII 13 (0x0D)), a carriage return.
"##代码##" (ASCII 0 (0x00)), the NUL-byte.
"\x0B" (ASCII 11 (0x0B)), a vertical tab.
It's there to remove line breaks from different kinds of text files, but does not handle html.
它可以从不同类型的文本文件中删除换行符,但不处理 html。
回答by PYK
I use 3 lines to do this job, so consider $sas your "stuff"...
我使用 3 行来完成这项工作,因此将$s视为您的“东西”...
##代码##
