Reputation: 4123
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.
Upvotes: 200
Views: 429013
Reputation: 1
In my experience this problem is not in the replacing of characters but in understanding how HTML works. That is; my experience and my understanding. If you get an input string from a textarea an you have to save it in a db, you have to escape the "\r" and "\n" (and more characters) before you save it in a MySQL/MariaDB database with something like:
public static function EscapeMySQL(array $arrInput):array{
foreach($arrInput as $key=>$value){
if(!empty($value) AND is_string($value)){
$arrInput[$key]=str_replace('<br/>','\r', $value);
$arrInput[$key]=str_replace(array('\\',"\0","\n","\r","'",'"','`',"\x1a"),array('\\\\','\\0','\\n','\\r',"\\'",'\\"','\\`','\\Z'),$value);
}
}
return $arrInput;
}
But if you retrieve the data and put it in a textarea, you can just put is there. Texarea will interpret "\n" and or "\r" as in notepad or so (a linebreak). However if you put the text directly in a div or so you have to use the nl2br function.
Upvotes: 0
Reputation: 47883
The best, most direct, operating system agnostic approach is to avoid calling nl2br()
entirely and just replace newline sequences with <br />
tags.
\R
line break: matches\n
,\r
and\r\n
\R
is equivalent to (?>\r\n|\n|\r|\f|\x0b|\x85)
.
Code: (Demo)
$html = <<<HTML
Dear friends, I just wanted so Hello. How are you guys? I'm fine, thanks!
Greetings,
Bill
HTML;
echo preg_replace('/\R/', '<br />', $html);
Output (exactly as desired, in one step):
Dear friends, I just wanted so Hello. How are you guys? I'm fine, thanks!<br /><br />Greetings,</ br>Bill
Upvotes: 1
Reputation: 47
Use this code to replace \r\n
str_ireplace(array("\r","\n",'\r','\n'),'', $row['smalldescp']);
Upvotes: -2
Reputation: 15
after trying literally all of these solutions.. the only thing that worked was to (urlencode - then replace - then urldecode)
$jsonDump = urlencode($jsonDump);
$jsonDump = str_replace("%5Cr","",$jsonDump);
$jsonDump = str_replace("%5Cn","",$jsonDump);
$jsonDump = urldecode($jsonDump);
Upvotes: -1
Reputation: 11233
Something a bit more functional (easy to use anywhere):
function strip_carriage_returns($string)
{
return str_replace(["\n\r", "\n", "\r"], '', $string);
}
Using PHP_EOL as the search replacement parameter is also a good idea! Kudos.
Upvotes: 12
Reputation: 171
To work properly also on Windows I'd suggest to use
$buffer = str_replace(["\r\n", "\r", "\n"], "", $buffer);
"\r\n"
- for Windows, "\r"
- for Mac and "\n"
- for Linux
Upvotes: 13
Reputation: 166359
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(["\r\n", "\r", "\n"], "<br />", $string);
Upvotes: 15
Reputation: 182
I think my answer is too late but I got same extra line issue while using it in JS script, but it works like this
This will add <br />
and line breaks in string.
$text = nl2br($text);
This line will remove extra line.
$text = preg_replace("/\r|\n/", "", $text);
Replacing <br />
with any unwanted common symbol like this asterisk **
while inserting in DB:
$text = str_replace("<br />","**",$text );
And when I want to use it in program that time I use it replacing **
.
$text = str_replace("**","\\n",$text );
Then use $text
in program
Upvotes: 2
Reputation: 107
You can use preg_replace in order to replace parts of a string matching a regular expression, like:
preg_replace("/\s+/","",$string);
Upvotes: -1
Reputation: 4291
I use 3 lines to do this job, so consider $s as your "stuff"...
$s=str_replace(chr(10),'',$s);
$s=str_replace(chr(13),'',$s);
$s=str_replace("\r\n"),'',$s);
chr(10)___a line feed
chr(13)___the return
\r\n______a new line
Upvotes: 1
Reputation: 13067
Alternative built-in: 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\0\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.
"\0" (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.
Upvotes: 0
Reputation: 7426
You should be able to replace it with a preg that removes all newlines and carriage returns. The code is:
preg_replace( "/\r|\n/", "", $yourString );
Even though the \n
characters are not appearing, if you are getting carriage returns there is an invisible character there. The preg replace should grab and fix those.
Upvotes: 445
Reputation: 1027
You can also use PHP trim
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.
Upvotes: 8
Reputation: 8037
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.
Upvotes: 611
Reputation: 16462
$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
Upvotes: 19