Jump to content

Full HTML Form textbox Editor - Formatted for PHP


alexville

Recommended Posts

Hello,

 

I was wondering if there is a such thing that lets users input text into a html form input box, but when they start a new line or something, it inputs "<p>" when is outputed to php. So that the text that they inputed will look exactly the same on a php's output. Its sort of like this forum's way of inputting text.

Link to comment
Share on other sites

Kind of on the same topic...

 

Is there a way to display text exactly as the user entered it in a form after it has been saved to the database.  I'm having a problem now that if you enter a few paragraphs of info into a form field, then display the data onto another page from the database, the paragraphs are all together with no lines in between each paragraph.  If I enter a break tag after a paragraph or put paragraph tags around each paragraph, the info is diplayed correctly. 

Link to comment
Share on other sites

You need to process the input from the user and replace line breaks with BR tags. You can do this before saving to the database or do it when you display the databse results to the page.

 

The other option is to display using PRE so that the text is displayed exactly as it was enetered. But, you lose some functionality related to display - I find it mostly useful for displaying code to a page.

Link to comment
Share on other sites

You need to process the input from the user and replace line breaks with BR tags. You can do this before saving to the database or do it when you display the databse results to the page.

 

The other option is to display using PRE so that the text is displayed exactly as it was enetered. But, you lose some functionality related to display - I find it mostly useful for displaying code to a page.

 

What would you use to process the BR tags?

Link to comment
Share on other sites

You need to process the input from the user and replace line breaks with BR tags. You can do this before saving to the database or do it when you display the databse results to the page.

 

The other option is to display using PRE so that the text is displayed exactly as it was enetered. But, you lose some functionality related to display - I find it mostly useful for displaying code to a page.

 

What would you use to process the BR tags?

 

There's always nl2br (http://us2.php.net/manual/en/function.nl2br.php).

 

I usually take the arguably unnecessary step of running everything though a series of regular expressions, for example:

 

$sOutput = preg_replace('/\\n/', '<br />', $sInput);

 

The above is basically a waste of time since it's functionally what nl2br does (as far as I'm aware). Sometimes, I'll do this instead:

 

$sOutput = preg_replace('/\\n\\n/si', '<\p><p>', $sInput); // Do this first to capture any double newlines.
$sOutput = preg_replace('/\\n/si', '<br />', $sInput); // Assume single newlines are meant to be breaks, not paragraphs.
$sOutput = "<p>".$sOutput."</p>";

 

I haven't tested any of the above, but I'm pretty sure that'll work...Might be overkill. Anyway, that's my two cents.

 

Oh yeah: if you wanted to make sure multiple newlines were treated as just a new paragraph, you could change the above code to this:

 

...
$sOutput = preg_replace('/\\n(\\n+)+/si', '</p><p>', $sInput); // Do this first to capture any multiple (more than one) newlines.
...

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.