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);
Related
I'm trying to store user messages from a textarea, into a text file, along with the user's name and date(though lets ignore the date for now).
Lets say the the user, named John, enters the following text into the textarea:
Hello
How are you?
I want the text file to store this as:
John#Hello\nHow are you?
I have tried many ways to remove/replace the newline from the textarea, but the result I get in the text file always looks like this:
John#Hello
How are you?
I just can't make it not go to the next line in the file. I've tried searching and haven't seen anyone having this problem...so perhaps I don't know what I'm doing. Anyone have a solution?
To replace the newline with a literal \n, use str_replace and put \n in single quotes so it's not interpreted as an escape sequence.
$text = str_replace("\n", '\n', $text);
file_put_contents("filename.txt", "$username#$text");
What is the difference between single-quoted and double-quoted strings in PHP?
Alternatively, you could escape the backslash: "\\n"
Use preg_replace("#[\n\r]+#", '\\n', $text);
I am using PHP/mysqli to read in comments, but various comments in the table have either a single quote or a double quote.
I am storing the comments in a data-attribute. Using the Chrome console, I can see where the quote is throwing the whole code out of whack.
<?php
echo "<td><a href='' class='comment' data-toggle='modal' data-comment='".htmlentities($row[comment])."'>" . $row[partner_name] . "</a></td>";
?>
As you can see in the code above, I tried to use htmlentities. I also tried addslashes and a combination of the two.
Either way, I still can't get the comment to display properly because of the quote inside the mysql table.
Is there another PHP function that I can use to fix this?
Directly above is a screen shot from the Chrome console. Right after the words POTENTIAL 53 there is a single quote that is throwing my code off. All the other orange text is being read as HTML when it's supposed to be part of the comment.
There has to be a way to read the single quote as part of the string.
Pass the flag, ENT_QUOTES, to your htmlentities function. See http://php.net/htmlentities. This will replace quotes with entified quote and prevent it from breaking out of the data-comment attribute.
Well, there are two problems:
You have to encode stuff, especially quotes:
$text = htmlentities($value, ENT_QUOTES);
The title attribute does not work with newlines, so you will have to deal that. Something like this should do the job:
$text = preg_replace('/\r?\n/', '#xA;', $text);
Try escaping the quotes in your data. Something to this affect:
$pattern = "/\"|\'/";
$replace = '\\\"';
$subject = $row[comment];
$rowComment = preg_filter($pattern, $replace, $subject);
*Tip - You can also filter the data before storing it.
Description: echo $rowComment will produce a string with all quotes escaped;
I use a textarea to write comments on my website. The comment is saved in a SQLite DB.
My problem is when I try to retrieve my comment from the DB in order to replace every carriage return with <p> tags (before showing it to the user).
I've first try the nl2br function and it works fine, plenty of <br/> appears on my code.
Then I've try :
substr_count($article->texte, '\n');
substr_count($article->texte, '\r');
But the return result is always 0. It surprises me because I thought nl2br would replace \n and \r chars.
Did I miss something ?
mb_detect_encoding($article->texte); //returns UTF8
You need to understand that PHP interprets text inside single quotes literally, but expands what is inside double quotes; so you will get a different result if you do
substr_count($article->texte, "\n");
To answer your question, using nl2br is quickest, but if you really want to replace every occurrance of "\n" with "</p><p>" then do:
$content = str_replace("\n", '</p><p>', $content);
Expressions like \n and \r are evaluated only when in double quotes, so try "\n" and "\r"
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.
I have a php string with a lot of information to be displayed inside a textarea html element.
I don't have access to that textarea nor to the script (if any) that generates it.
$somestring = 'first line \nSecond line \nThird line.';
$somestring as NOT been "worked" with trim or filter_var. Nothing.
On the textfield, I get the \n printed on the textarea hence, not interpreted.
What can I try in order to have those new lines applied?
Thanks in advance.
Try wrapping $somestring with " (double quotes) instead of ' (single quotes)
\n, \r and other backslash escape characters only works in double quotes and heredoc. In single quotes and nowdoc (the single quote version of heredoc), they are read as literal \n and \r.
Example:
<?php
echo "Hello\nWorld"; // Two lines: 'Hello' and 'World'
echo 'Hello\nWorld'; // One line: literally 'Hello\nWorld'
echo <<<HEREDOC
Hello\nWorld
HEREDOC; // Same as "Hello\nWorld"
echo <<<'NOWDOC'
Hello\nWorld
NOWDOC; // Same as 'Hello\nWorld' - only works in PHP 5.3.0+
Read more about this behaviour in the PHP manual
EDIT:
The reason single and double quotes behave differently is because they are both needed in different situations.
For instance, if you would have a string with a lot of new lines, you would use double quotes:
echo "This\nstring\nhas\na\nlot\nof\nlines\n";
But if you would use a string with a lot of backslashes, such as a file name (on Windows) or a regular expression, you would use single quotes to simplify it and avoid having unexpected problems by forgetting to escape a backslash:
echo "C:\this\will\not\work"; // Prints a tab instead of \t and a newline instead of \n
echo 'C:\this\would\work'; // Prints the expected string
echo '/regular expression/'; // Best way to write a regular expression
$somestring = "first line \nSecond line \nThird line.";
http://php.net/types.string <-- extremely useful reading
this article is a cornerstone of PHP knowledge and it's just impossible to use PHP without it.
unlike most of manual pages which are are just for quick reference, this very page is one which every developer should learn by heart.