Jump to content


Photo

returning preformatted text from a textarea to insert into a database


  • Please log in to reply
8 replies to this topic

#1 robcrozier

robcrozier
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 16 August 2006 - 10:27 AM

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???

#2 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 16 August 2006 - 10:33 AM

I think wordwrap is what you are looking for. So, for instance if you want the text to be 50 spaces wide then you would do this.

$newtext = wordwrap($text, 50, "<br />\n");

Hope this helps.
TOm

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#3 robcrozier

robcrozier
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 16 August 2006 - 10:37 AM

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.

#4 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 16 August 2006 - 10:48 AM

I don't know. That is a good question. I will test a few things and see if I can figure it out.

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#5 robcrozier

robcrozier
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 16 August 2006 - 10:52 AM

Thanks mate, PLEASE let me know if you come up with anything ........  its driving me mad now!!! >:(

#6 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 16 August 2006 - 11:26 AM

Use nl2br this convets line breaks into their HTML equivalent (< br /> (without the space)).

#7 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 16 August 2006 - 12:27 PM

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

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

btw, nl2br() is the equivalent of
<?php
function nl2br ($string) 
{
    return str_replace("\n", "<br />\n", $string);
}
?>


#8 robcrozier

robcrozier
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 16 August 2006 - 01:43 PM

Cheers, that worked!  ;D

#9 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 16 August 2006 - 02:41 PM

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

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

btw, nl2br() is the equivalent of
<?php
function nl2br ($string) 
{
    return str_replace("\n", "<br />\n", $string);
}
?>

It doesnt just replace the \n character but '\r\n' (Windows), '\r' (Mac) and '\n' (Linux)





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users