1

For example

I use

$content = nl2br($_POST['content']);

and when I type in something like this in my form

"I'll be going to the office today"

It'll return

"I\'ll be going to the office today"

Is there a way I can remove the \'s? or am I using the nl2br function wrong?

Zera42
  • 2,592
  • 1
  • 21
  • 33

3 Answers3

4

nl2br() does no such thing! You have magic quotes on. Turn them off.

Community
  • 1
  • 1
Brad
  • 159,648
  • 54
  • 349
  • 530
  • I'm assuming magic quotes are server sided? Could I just use the solution above with the stripslashes? – Zera42 May 30 '13 at 04:13
  • @SieuPhan, Yes, it is server-side. You really should turn magic quotes off in the first place, but if you don't care about hackish cluttered code and adding unnecessary overhead, or you are on shared hosting and cannot edit your config, then read the instructions on the link in my answer. – Brad May 30 '13 at 04:18
0

I'm guessing you're getting information via a POST or GET; try something like this:

<?php
if (get_magic_quotes_gpc()) {
    $process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
    while (list($key, $val) = each($process)) {
        foreach ($val as $k => $v) {
            unset($process[$key][$k]);
            if (is_array($v)) {
                $process[$key][stripslashes($k)] = $v;
                $process[] = &$process[$key][stripslashes($k)];
            } else {
                $process[$key][stripslashes($k)] = stripslashes($v);
            }
        }
    }
    unset($process);
}
?>

More information on the PHP manual

faino
  • 3,194
  • 15
  • 17
0

Try to use stripslashes( $content ).

Kuo Ming Lin
  • 143
  • 2
  • 9
  • I don't really see the purpose of your `Quotes` class. All you're doing is wrapping the built-in functions. Not only that, but you've added a bug. Your `unescapeHtmlString()` method double-unescapes `>` and `<`. Finally, your code has no license attached to it, which means that at least in the US, it cannot be used as you own all of the rights to it. If you want someone to be able to use your code, consider attaching a license to it. http://opensource.org/licenses – Brad May 30 '13 at 04:27
  • Hi, Brad. I need to explain the bug. Why double use str_replace() to unescapes < and > ? Because of the htmlspecialchars() will be escaping the < and > symbols actually, but it won't be really decode from using htmlspecialchars_decode(), when it displays on browser that it'll be fine, but it'll be damaged when we need to catch the source code likes use json_encode() to response some web-service requests. Anyway, thanks for your advice, it really help me to consider rules of open source. – Kuo Ming Lin May 30 '13 at 06:20
  • `htmlspecialchars_decode()` does take `<` and `>` and converts them to `<` and `>`. – Brad May 30 '13 at 15:40