How to remove line breaks (no characters!) from th

2019-01-01 15:06发布

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:

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

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.

I am using nl2br() on it to generate HTML output, but that's not enough. The result then is:

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?

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.

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.

9条回答
君临天下
2楼-- · 2019-01-01 15:39

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_EOL as the search replacement parameter is also a good idea! Kudos.

查看更多
像晚风撩人
3楼-- · 2019-01-01 15:42

Ben's solution is acceptable, but str_replace() is by far faster than preg_replace()

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

Using less CPU power, reduces the world carbon dioxide emissions.

查看更多
长期被迫恋爱
4楼-- · 2019-01-01 15:45

It's because nl2br() doesn't remove new lines at all.

Returns string with <br /> or <br> inserted before all newlines (\r\n, \n\r, \n and \r).

Use str_replace instead:

$string = str_replace(array("\r\n", "\r", "\n"), "<br />", $string);
查看更多
只若初见
5楼-- · 2019-01-01 15:49
$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
查看更多
倾城一夜雪
6楼-- · 2019-01-01 15:51

To work properly also on Windows I'd suggest to use

$buffer = str_replace(array("\r\n", "\r", "\n"), "", $buffer);

"\r\n" - for Windows, "\r" - for Mac and "\n" - for Linux

查看更多
深知你不懂我心
7楼-- · 2019-01-01 15:54

str_replace(PHP_EOL, null, $str);

查看更多
登录 后发表回答