0

I use mysqli_real_escape_string() when users enter data, I have a private messaging system and each message they enter is escaped using the function. Now, the data entered is text and is saved into a database. So when I retrieve it from the database and echo it, it puts backslashes and certain letters in it. So for example..

Here's a couple...not sure which one will work best though...

Displays as..

Here\'s a couple...not sure which one will work best though...\r\n\r\

How can I prevent the slashes and r/n letters from appearing?

Stripslashes() changes it to this..

Here's a couple...not sure which one will work best though...rnrn

I still need the letters to go

heymck93
  • 23
  • 5

2 Answers2

0

Use stripslashes to remove \'

$str = "Is your name O\'reilly?\n";

// remove new lines
$str = str_replace(array("\n", "\r"), array('', ''), $str);

// Outputs: Is your name O'reilly?
echo stripslashes($str);

Also, please note that you should avoid using PHP's mysql* functions. See here

Community
  • 1
  • 1
ggirtsou
  • 2,050
  • 2
  • 20
  • 33
0

use nl2br() with stripslashes()

nl2br — Inserts HTML line breaks before all newlines in a string

nl2br(stripslashes($result));

it will return string with <br /> or <br> inserted before all newlines (\r\n, \n\r, \n and \r).

A.B
  • 20,110
  • 3
  • 37
  • 71