Page 1 of 2

Turn blank lines into <br>?

Posted: Sat Sep 02, 2006 1:08 pm
by toasty2
In my CMS, I need it to change the places where people make blank lines into <br>, because otherwise the lines will have no effect.
Lets say a user submits
$_POST['body'] wrote:Hi

Bye
That input would need to change to

Code: Select all

Hi<br>
<br>
Bye
So, any ideas?

Re: Turn blank lines into <br>?

Posted: Sat Sep 02, 2006 1:09 pm
by toasty2
Oops. double post. :(

Posted: Sat Sep 02, 2006 2:04 pm
by jayshields

Code: Select all

nl2br()

Posted: Sat Sep 02, 2006 2:06 pm
by toasty2
Thanks, I'll look into that.

Edit: Works perfect, thanks a ton.

Is there any way I can reverse it, for when people edit the pages? They'll be confused when they see <br> inside their pages.

Posted: Sat Sep 02, 2006 2:34 pm
by Ollie Saunders
Only use it on output, don't modify the files in the first place.

Posted: Sat Sep 02, 2006 2:41 pm
by toasty2
I never thought of that. I'll do that, thanks Ole.

Edit: that's not too helpful, it's spacing out my content weird.
Edit2: nevermind, if I remove all the previous <br>'s, it works fine. It's still kind of weird to me though, I'm not used to it :P

Posted: Sun Sep 03, 2006 1:14 pm
by toasty2
I don't think its necessary to start a new thread for this...I have another question/problem.
When I submit things like "I'm", it saves it as "I\'m". How can I fix this?

Posted: Sun Sep 03, 2006 1:17 pm
by jayshields

Code: Select all

stripslashes()

Posted: Sun Sep 03, 2006 1:29 pm
by toasty2
Thanks. I knew there was a function, I just couldn't remember the darn thing.

Posted: Sun Sep 03, 2006 1:59 pm
by Ollie Saunders
Again you should only be adding slashes at the time it is required. Like when you put it into the query etc.
You should never (if you write your code correctly) have to call stripslashes().

Posted: Sun Sep 03, 2006 2:16 pm
by toasty2
I don't use databases.

Posted: Sun Sep 03, 2006 2:26 pm
by Ollie Saunders
Why do you have slashes in your code then? magic_quotes_gpc?

Posted: Sun Sep 03, 2006 2:34 pm
by toasty2
Well, apparently so :P
Thats what a quick run of phpinfo() told me. Stupid shared hosting.

Posted: Sun Sep 03, 2006 3:20 pm
by Ollie Saunders
Seeing as I'm writing a library like you, I did a bit of research. In PHP 5 you'll want to do this:

Code: Select all

static public function antiMagicQuote()
{
    if (get_magic_quotes_gpc()) {
        $superStrip = create_function('&$toStrip', '$toStrip = stripslashes($toStrip)');
        $_POST   = array_walk_recursive($superStrip, $_POST);
        $_GET    = array_walk_recursive($superStrip, $_GET);
        $_COOKIE = array_walk_recursive($superStrip, $_COOKIE);
    }
}
You might want to test that. Although it should work for $_POST['var'] and $_POST['var']['subVar']. Well that was the idea.
PHP 4 you will have to settle with:

Code: Select all

if (get_magic_quotes_gpc()) {
    function stripslashes_deep($value)
    {
        $value = is_array($value) ?
                    array_map('stripslashes_deep', $value) :
                    stripslashes($value);

        return $value;
    }

    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
}
?>
That last one isn't mine.

In my library I'm actually triggering an error if magic_quotes is on and allowing antiMagicQuote() to be called manually by the user.

Posted: Sun Sep 03, 2006 3:51 pm
by Ollie Saunders
And here is a version that works :D:lol:

Code: Select all

static public function antiMagicQuote()
    {
        $superStrip = create_function('&$toStrip', '$toStrip = stripslashes($toStrip);');
        array_walk_recursive($_POST, $superStrip);
        array_walk_recursive($_GET, $superStrip);
        array_walk_recursive($_COOKIE, $superStrip);
    }

Code: Select all

$_POST['foo'] = array('a' => '\"', 'b' => '\"');
        $_POST['bar'] = '\"';
        OF::antiMagicQuote();

        $this->assertEqual($_POST['foo']['a'], '"');
        $this->assertEqual($_POST['foo']['b'], '"');
        $this->assertEqual($_POST['bar'], '"');