Jump to content

returning preformatted text from a textarea to insert into a database


robcrozier

Recommended Posts

ok guys here's the situation...

i want to be able to input preformatted text into a form textarea which i will then return as a variable to insert into my database.  This text block (complete with line feeds etc) will then be used in various other pages.  im ok with actually returning the variable from the form and inserting it into the database, however i can't return it as preformatted text with all line feeds etc in tact.  It just all mushes together into one big paragraph!!!

can any one help???
Link to comment
Share on other sites

I think [url=http://us3.php.net/manual/en/function.wordwrap.php]wordwrap[/url] is what you are looking for. So, for instance if you want the text to be 50 spaces wide then you would do this.

[code=php:0]$newtext = wordwrap($text, 50, "<br />\n");[/code]

Hope this helps.
TOm
Link to comment
Share on other sites

hi, thanks for that but i dont just want to determine the overall width of the text block to be returned from the textarea.  i want to return all the 'return key presses' that the user inputs into the textarea.  Meaning that the returned output will display exactly as the user has entered it into the textarea.  hope this makes it a bit clearer.
Link to comment
Share on other sites

[code]<?php
$query = "INSERT INTO `table` VALUES ('" . mysql_real_escape_string($_POST['textarea']) . "')";

echo '<textarea>' . nl2br(htmlentities($_POST['textarea'])) . '</textarea>';
?>[/code]

btw, nl2br() is the equivalent of [code]<?php
function nl2br ($string)
{
    return str_replace("\n", "<br />\n", $string);
}
?>[/code]
Link to comment
Share on other sites

[quote author=Jenk link=topic=104474.msg416770#msg416770 date=1155731251]
[code]<?php
$query = "INSERT INTO `table` VALUES ('" . mysql_real_escape_string($_POST['textarea']) . "')";

echo '<textarea>' . nl2br(htmlentities($_POST['textarea'])) . '</textarea>';
?>[/code]

btw, nl2br() is the equivalent of [code]<?php
function nl2br ($string)
{
    return str_replace("\n", "<br />\n", $string);
}
?>[/code]
[/quote]
It doesnt just replace the \n character but '\r\n' (Windows), '\r' (Mac) and '\n' (Linux)
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.