Jump to content

php html into db table


bradkenyon

Recommended Posts

got a question, say you have a textarea, and you put content in there and throw it into a db table, then display it on webpage, if the user hits enter to put a line break in the textarea box of the form, how do i tell it to put it into the db as a line break, and display it on the webpage as a line break.

Link to comment
Share on other sites

i am making a quick content management system and i want to allow the user to press enter within the text field (textarea) and it will create a line break, like you would in any typical word processor. So it will display the content they typed into the field as w/ the line break from when they pressed enter.

 

i hope i didn't make it too confusing.

Link to comment
Share on other sites

nl2br is your best best for this.

 

If you want paragraph breaks as well, trying using regex

 

<?php

$regex = array (
    '/(\\r\\n\\r\\n|\\n\\n)/',
    '/(\\r\\n|\\n)(?!\\r\\n|\\n)/',
    '/_NL_/'
);
$replace = array (
    "</p>_NL_<p>",
    "<br />_NL_",
    "\n"
);

$subject = 'This is a
new line

This is a new
paragraph

Another
paragraph';

$result = preg_replace($regex, $replace, $subject);

echo "<p>$result</p>";


?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.