Reputation: 69
I'm trying to make text input in a textarea as easy as possible for the user, without them needing to know any code or have to deal with wysiwyg formatting. It's just text. But since the text, when shown on the page, will be shown in html, it would be great for there to be 'p' tags around each line.
Right now I've tried this:
$content = mysqli_real_escape_string($connection,$_POST['content']);
$paragraphs = explode("\n", $content);
for ($i = 0; $i < count ($paragraphs); $i++)
{
$paragraphs[$i] = '<p>' . $paragraphs[$i] . '</p>';
}
$content = implode('', $paragraphs);
But this is putting the <p></p>
tags around EVERYTHING - one at the beginning of the post, and one at the end, ignoring all returns.
Can anyone see what I'm doing wrong?
Upvotes: 0
Views: 593
Reputation: 57131
When you call mysqli_real_escape_string()
, this encodes certain things - from the manual
escapestr
The string to be escaped.
Characters encoded are NUL (ASCII 0), \n, \r, \, ', ", and Control-Z.
So any \n
's will be escaped.
You could change it round to do the replacement first...
$paragraphs = explode("\n", $_POST['content']);
for ($i = 0; $i < count ($paragraphs); $i++)
{
$paragraphs[$i] = '<p>' . $paragraphs[$i] . '</p>';
}
$content = implode('', $paragraphs);
BUT you should also be using prepared statements, so you shouldn't need to call mysqli_real_escape_string()
at all.
Upvotes: 2