Jump to content

Archived

This topic is now archived and is closed to further replies.

robcrozier

returning preformatted text from a textarea to insert into a database

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

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
I don't know. That is a good question. I will test a few things and see if I can figure it out.

Share this post


Link to post
Share on other sites
Thanks mate, PLEASE let me know if you come up with anything ........  its driving me mad now!!! >:(

Share this post


Link to post
Share on other sites
Use nl2br this convets line breaks into their HTML equivalent (< br /> (without the space)).

Share this post


Link to post
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]

Share this post


Link to post
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)

Share this post


Link to post
Share on other sites

×

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.