Turn blank lines into <br>?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Turn blank lines into <br>?

Post 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?
Last edited by toasty2 on Sat Sep 02, 2006 1:35 pm, edited 2 times in total.
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Re: Turn blank lines into <br>?

Post by toasty2 »

Oops. double post. :(
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Code: Select all

nl2br()
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Only use it on output, don't modify the files in the first place.
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post 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
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post 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?
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Code: Select all

stripslashes()
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

Thanks. I knew there was a function, I just couldn't remember the darn thing.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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().
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

I don't use databases.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Why do you have slashes in your code then? magic_quotes_gpc?
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

Well, apparently so :P
Thats what a quick run of phpinfo() told me. Stupid shared hosting.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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'], '"');
Post Reply