Reputation: 4323
Can somebody please help me with this problem.
I'm simply trying to display a message from users. The message comes to my database from a textarea. My problem is when I am typing message and formatting it with breaking into paragraphs it is not display in that format and display it as a one long paragraph.
like this.
To change the overall look of your document, choose new Theme elements on the Page Layout tab. To change the looks available in the Quick Style gallery, use the Change Current Quick Style Set command. \r\n\r\nBoth the Themes gallery and the Quick Styles gallery provide reset commands so that you can always restore the look of your document to the original contained in your current template.
I tried with nl2br() and strip_tags() but still no luck
The Code from validation message.
// Check for message
if ( !empty ( $_POST['message'])) {
if ( mb_strlen ( $_POST['message'], 'UTF-8') <= 20 ) {
$reg_errors['message'] = 'Your message must be greater than 20 characters';
} else {
$message = mysqli_real_escape_string($dbc, $_POST['message']);
}
} else {
$reg_errors['message'] = 'Message: This field is required.';
}
This is the code I use when select message from db
$message= $row['message'];
$message= nl2br(strip_tags($message));
when echoing $message it print a paragraph like above.
Upvotes: 0
Views: 3483
Reputation: 157863
You are doing excessive escaping when adding data into database.
If you are using prepared statements, just remove mysqli_real_escape_string() call from your code.
Otherwise you are doing it twice - som you have to find the place where it happens and remove extra mysqli_real_escape_string() call from your code.
Upvotes: 2
Reputation: 7470
From my understanding, by using mysqli_real_escape_string()
you are making it escape the backslashes so at the time of transaction for example \r\n
becomes \\r\\n
, however, if those have already been escaped by Javascript then what you end up during transaction is \\\\r\\\\n
which translates back into html as
So you might want to replace them with appropriate characters before adding into your database. Doing that you can pic from a variety of built-in functions such as preg_replace()
, strtr()
, str_replace()
…
Upvotes: 0