On my home page, I display some info about the hikes (it is a hiking site) and embarassinlgy,
the \r\n characters still show up.
I do these functons
$hike_description = htmlspecialchars ($hike_description);
$hike_description = nl2br($hike_description);
And still those characters do not go away. You can take a look yourself at http://www.comehike.com
Would you know what is the proper way to get rid of the \r\n characters? You can see it happening in the "Upcoming Hikes" section...on the 3rd hike.
Thanks!
In your case, the following will work:
$hike_description = str_replace ( "\\r\\n","<br />", $hike_description);
The text \r\n is literal, rather than control characters.
Try manually replacing the sequence
str_replace( "\r\n", "<br />", $hike_description );
The \n\r in your page are not escape sequences... They are actually a \ character followed by a n character followed by a \ character followed by a r character.
In your database, you should store that as the actual characters, not the escape sequences. Then, calling nl2br() will work as expected.
Yes, you can do a str_replace(), however, you should instead fix the encoding of your data in your database. It will save you trouble in the future.
Related
I have an issue. I'm trying to write a string with ASCII text like this: '/\'. But whenever I do that the backslash screws up the code by canceling out the quote defining it a string therefore screwing it up. Is there anyway to cancel out the backslash so it doesn't cancel out the quote? Thanks guys!
The \ is special character, that says: 'The next character has special meaning'.
So if you want to dispaly \ you should write... \\ to get one \ in output
It would be very helpful to show what you have tried, but this will produce the exact output you requested (as shown by SO)
echo '\'/\\' . "'\n" ;
'/\'
It should also give you an idea of how backslash escaping works in different types of strings.
A great solution when writing stuff like that is HEREDOC. Inside a heredoc block you don't need to worry about escaping anything, it will just be text.
For example:
echo <<<TEXT
/|\/|\/|\/|\/|\/|\/|\/|\/|\/|\
TEXT;
There is one catch. PHP will break if you don't align the echo at the start of the line, or if the TEXT; is not aligned at the start of the line.
Heredoc can also be assigned to a variable, like so:
$var = <<<SOME_MORE_TEXT
/|\/|\/|\/|\/|\/|\/|\/|\/|\/|\
SOME_MORE_TEXT;
Finally, HEREDOC preserves tabs and spaces. Which also might come in handy when doing ASCII art.
Refer to: http://php.net/manual/en/language.types.string.php for more information.
You only need to escape the final one when using single quotes.
$var = 'backslash\backslash\backslash\\';
// output is:
// backslash\backslash\backslash\
For some reason my server is ignoring "\t" -- yes I am using double quotes.
When I echo out a string like "abc\tdef\tghi" the "\t" is shown as a small "HT" with the "H" slightly smaller and above the "T"
Oddly, "\n" works and gives me a new line.
Not sure if this is an issue with PHP, NGINX, or something else, any ideas?
Try to use the the escape character instead the escape sequence. This works like a charm and you don't neet to worry about anything.
Let's say you have something like this:
$text = "First\tSecond\tThird\t...";
echo $text;
Replace it with this:
$text = "First\x09Second\x09Third\x09...";
echo $text;
Please note that using 'single quotes' with escape characters will get you '\x09' instead a real tabulator!
A list of all escape characters can be found in php.net manual:
PHP.NET escape characters
I have searched throughout this site and google, and could not find a viable solution. There are solutions, but none seem to work for me.
I have a text area that serves as the input for a form, as well as the output (editable) when displaying the form to edit. So, I have a textarea like so:
echo '<textarea>'.$resolution.'</textarea>';
If I enter something with line breaks, it is interpreted as \r\n wherever there is a carriage return. Example:
Input:
This is a
test.
Output:
This is a\\r\\n\\r\\ntest.
Now, I found it simple to remove the extra slash by using stripslashes as follows:
$resolution = stripslashes($resolution);
...but, now the output is:
This is a\r\n\r\ntest.
I cannot figure out how to convert \r\n to a line break (that is, without using a tag, since that would only output <br> within the textarea, where html would not be supported. I've tried all of the following, but none of them worked:
//Effort 1
$resolution = trim($resolution);
//Effort 2
$resolution = nl2br($resolution);
//Effort 3
$resolution = htmlentities($resolution);
//Effort 4
$resolution = preg_replace("\\r\\n","<br>",$resolution);
I'm now at a complete loss. Can anyone shed some light on this?
PHP has two different string building modes. The first uses single quotes, and will do absolutely no variable or special character substitutions. That's what you're using.
The second is variable-embedding in double quoted strings, which should work:
$text = str_replace("\\", "\", $text);
echo "<textarea>$text</textarea>";
The \r and \n should now be active carriage return and newline characters in your output, not the character "slash" and then an 'r' or 'n'
Just use double quotes on str_replace
str_replace('\r',"\r",str_replace('\n',"\n",$resolution));
http://php.net/manual/en/language.types.string.php
or if you're really dealing with double backslashes:
str_replace('\\\\r',"\r",str_replace('\\\\n',"\n",$resolution));
I'm new to PHP (so no bullying please) and I'm trying to trim a variable to remove quotation marks. For some reason, my quotation marks are replaced by semicolons, anybody knows why ?
Where $movieArray["title"] = '"Veronica Mars"';
str_replace('"', '', $movieArray["title"]);
output: ;Veronica Mars;
Also, the reason I have this " instead of this " is that the trim doesn't work with ".
Thanks
Your text is not "Veronica Mars". It's probably this:
"Veronica Mars"
If you strip ", only the ; remains.
What you see in the browser screen is the result of rendering some HTML code.
I can't get nl2br function to work after fetching data from my database:
$result = mysql_query("SELECT comments..etc.etc..");
while ($row = mysql_fetch_array($result))
{
echo nl2br($row["comments"]);
}
In database row comments:
\r\nThanks,\r\n
OUTPUT:
Same as in DB:
\r\nThanks,\r\n
If I simply test this out like so it works fine:
<?php
$mystring = "\r\nThanks,\r\n";
echo nl2br($mystring);
?>
OUTPUT:
converts \r \n to <br />
try this:
echo preg_replace('/\v+|\\\r\\\n/Ui','<br/>',$row["comments"]);
I know this is an old post but if like me you are have stumbled across this issue and the above didn't work for you, this solution may help you instead:
echo nl2br(stripslashes($row["comments"]));
or (they are not the same function, note the additional "c" after strip)
echo nl2br(stripcslashes($row["comments"]));
See original thread that helped me: nl2br() not working when displaying SQL results
Most likely you are doing escaping twice, when adding your data into DB.
Check your code that adds data to DB and remove unnecessary escaping.
Most likely it's some senseless "universal sanitization" function.
Well it's easy.
Let's take a quote, not a newline to demonstrate. The behavior the same.
Slashes being stripped then data goes to database.
Thus, in the normal case:
source: It's
after escaping: It\'s
by the query execution slash being stripped and
both in the database and back It's
in double escaping case:
source: It's
after escaping: It\'s
after second escaping: It\\\'s
by the query execution slash being stripped and
both in the database and back It\'s
we have our data spoiled.
Just make yourself understand that escaping i not something magical that makes your data "safe" (and, therefore can be done many times, as you probably think). It's just adding a backslash to certain symbols.
My guess is that the slashes in your DB are literal slashes (followed by n or r), not newlines. Can you find a way to store literal newlines in your database?
Following solution will work for both windows as well as for linux/unix machine
str_replace(array("\\r\\n", "\\r", "\\n"), "<br />", "string");
Make sure that you are not to using strings from file and/or set in single apostrophe. Because string is treated literally, and nl2br will not work.
NL2BR will work with double apostrophe.
Building on what Christian is saying, why don't you trying replacing the literal '\r\n' with "\r\n"?
Data you have stored is allready added the slashes.
You have to use stripslashes() first then str_replace()
stripslashes(str_replace('\r\n','<br/>',$row["comments"]))
For some reason this didn't work for me...
echo nl2br(stripcslashes($row["comments"]));
But this did...
$comments = stripcslashes($row["comments"]);
$commentsWithBreaks = nl2br($comments);
echo $commentsWithBreaks;
Not working for me either. I just did the following:
$mensaje = str_replace("
", "<br/>", $mensaje);
I was able to replace newline with <br> using this code :
str_replace(array("\r\n", "\r", "\n"), "<br>", "input");
(windows machine)
This could be a solution, at least it was for me.
$message = str_replace("\\r\\n", "<br>", $message);
It is possible that you are getting a string in which the slashes are escaped (i.e. \\) and nl2br works with \n\r not \\n\\r
Once you understand this, the solution is easy :)