I'm having some weird difficulty with removing "\n"'s from data that is pulled from a database.
The data is an email and gets stored with \n's throughout it by the system that inputs it.
When I display it, I've tried to remove these \n's using the following:
$htmlbody = str_replace("\n", "", $message['htmlbody']);
or
nl2br($message['htmlbody']);
but both commands still return a string that is full of \n's.
The variable $message['htmlbody'] includes a string like \n\n <div>\n Example Data \n </div>\n, and this data remains the same after being passed through str_replace.
The data originally comes from a JSON webhook which has replaced all the newlines in an HTML email with \n's. I have control over the data being put into the database as well, and have tried using the above actions on the original data with the same result.
Any thoughts on what might cause this?
Cheers.
Found this in a google search
$message = preg_replace("/\n|\r/", " ", $message['htmlbody']);
Which will replace newlines with whitespace.
to replace first \r\n then \r and then \n use this:
$message['htmlbody'] = preg_replace(array("/\r\n/", "/\r/", "/\n/"), array(' ',' ', ' '), $message['htmlbody']);
$message['htmlbody'] = str_replace("\n", "", $message['htmlbody']);
If the string you try to modify have \n displayed, and not interpreted as a new line, use :
$message['htmlbody'] = str_replace("\\n", "", $message['htmlbody']);
Try passing $message through stripslashes. It is possible that you have \\n\\r, that is double slashes which prevent correct escaping...
$message['htmlbody'] = nl2br(stripslashes($message['htmlbody']))
Related
I am trying to remove these \n\n characters that appear in my string when I post a form.
My code is;
$text = $textdb['columnname'];
echo trim(preg_replace('/\s\s+/', ' ', $text));
But it doesn't work. I have tried literally every example here and still nothing works. Am I suppose to do something prior to this? Does this work with a certain version of php? Am using 5.6 by the way. Thanks.
Just use nl2br
Example:
$text = nl2br("hey\n neighbor");
would print
hey
neighbor
Any way i can change it to use \n instead? I'm trying to save space.
This is on a Linux server.
$txt = $textbox'
$txt = str_replace("\r", '', $txt);
this will replace all the \r from the string then you can later do with it what ever you need
A simple way would be use preg_match to confirm the existence of '\r\n' first, and then only use preg_replace to replace with '\n'. Your regex pattern could be as simple as '/\r\n/', depends on the data.
I have a jquery editable div that when you click on it you can edit the text. The problem is that when the data is called from the db and placed into the paragraph I keep getting a \n for every space. How can I replace the \n with an actual new line.
I tried nl2br(), but that's not very convenient for my users since they then have to play with the <br /> when they want to edit the paragraph.
Any thoughts?
What about:
str_replace("\\n", "", $str); // see if this gets rid of them
Then this should work to put actual newlines in there:
str_replace("\\n", "\n", $str); // should replace with actual newline
try:-
$strippedText = str_replace(chr(10), '', $textFromDB);
or
$strippedText = str_replace(chr(10), '<br/>', $textFromDB);
Does this work? (Working on the possiblity that the newlines are already escaped).
$strippedText = str_replace('\\n', ' ', $textFromDB);
Are you using a ready made solution or making your own? I use http://aloha-editor.org/ for stuff like this and it's mostly problem free.
Have you tried str_replace?
$myTextFromDB = str_replace('\n', PHP_EOL, $myTextFromDB);
Ok, I think this is different enough that I should do a separate answer for it.
Are you saying a literal "slash n" shows up on the page? Or are you saying that your newlines show up as spaces?
If it's the latter, then there's no way around that. HTML will show newlines only as a space - you have to convert to br tags to break the line if it's not in a textarea context. But you can always convert them back to newlines when you pop that textarea up for your user and this should work well for people.
I am retrieving a product description value stored in database from admin through textarea upon form submit. When I select the description from database I get $description = $row['description']; and I would like to echo $description on main page like this: echo nl2br($description); but I see "\r\n" characters instead of making new rows. From what I've found here and on the net, your string must be used between double quotes, like this:
echo nl2br("Hello, \r\n This is the description");
Now, the value of $description from database is in fact "Hello, \r\n This is the description" but in my script I have to use it like this:
echo nl2br($description);
Which does not make br's, it is outputing \r\n instead. So, what can I do, I can't use double quotes here, from my experience.
You could translate them into their respective escape sequences before passing the string through nl2br(), like this:
$description = nl2br(str_replace('\\r\\n', "\r\n", $description));
But what are the literal escapes doing in your database in the first place?
You are storing the literal value of \r\n in your database, not the actual characters they represent.
Verify this in your database. If you see \r\n in the description field, then you're probably escaping the backslash when you're storing the data.
It looks like your text contains the individual characters \, r, \, and n, and does not contain actual newline characters. As such, str_replace() should get the job done:
echo str_replace('\r\n', '<br>', $description);
The nl2br can take a second (optional) argument for "is_xhtml" which will convert the \r\n into a <br> for you. Just change your line to:
echo nl2br($description, TRUE);
I'm using str_replace and it's not working correctly.
I have a text area, which input is sent with a form. When the data is received by the server, I want to change the new lines to ",".
$teams = $_GET["teams"];
$teams = str_replace("\n",",",$teams);
echo $teams;
Strangely, I receive the following result
Chelsea
,real
,Barcelona
instead of Chealsea,real,Barcelona.
What's wrong?
To expand on Waage's response, you could use an array to replace both sets of characters
$teams = str_replace(array("\r\n", "\n"),",",$teams);
echo $teams;
This should handle both items properly, as a single \n is valid and would not get caught if you were just replacing \r\n
Try replacing "\r\n" instead of just "\n"
I would trim the text and replace all consecutive CR/LF characters with a comma:
$text = preg_replace('/[\r\n]+/', ',', trim($text))
I had the same issue but found a different answer so thought I would share in case it helps someone.
The problem I had was that I wanted to replace \n with <br/> for printing in HTML. The simple change I had to make was to escape the backslash in str_replace("\n","<br>",($text)) like this:
str_replace("\\n","<br>",($text))