View Full Version : Keep text formatting ?
Hi guys,
I have a form which has a text field area, so the user can enter alot of text.
When the user presses enter inside this box, it obviously moves down a line.
This line spacing is kept in the database, to which it is stored.
But how do I keep this line spacing when I go to 'echo' it back into a table cell.
I can 'echo' it out but it just writes it in one long line.
Is there something you can do to keep the line spacing ??
:thumbsup:
boywonder
05-04-2003, 08:08 PM
http://www.php.net/manual/en/function.nl2br.php
missing-score
05-04-2003, 10:16 PM
If you want xhtml compliance (not sure if nl2br() does this) you may want to use preg_replace to use <br /> instead of <br>:
$text_value = preg_replace("/\\n/", "<br />", $text_value);
Nightfire
05-05-2003, 12:49 AM
nl2br does use the xhtml way :)
Hi guys
thanks for that, it was just what I wanted. I have another problem now though.
If when the user is entering the text they enter a really long line of text which is more than 45 characters, it stretches my table layout and messes it up.
So I need it to check each line and if it is more than 45 characters to put a line break in.
I found the word wrap feature but it doesn't seem to work as I need it to.
This is what I have:
<?php foreach ($articles as $art)
$string = nl2br ($art[article_text]);
$newstring = wordwrap($string,45,"<br>");
echo "$newstring"; ?>
But it just outputs this:
Hi ya
/>
hi there
/>
hello again
over here this
time
kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk kkkkkkkkkkkkkkkkkkk
Instead of breaking the k's at 45 characters and it puts the /> in there for some reason.
any ideas ?
:confused:
P.S It doesn't break the k's like it has here they remain one long line
missing-score
05-05-2003, 01:48 PM
<?php
foreach ($articles as $art)
{
$str = $art['article_text'];
$string = wordwrap($str, 45, "\n");
$string = nl2br($string);
echo $string;
}
?>
Try that.
No still doing the same thing missing score.
hmm
:confused:
No forget that its alright now. Its because it was all k's.
I just typed a load of junk, with some spaces as normal and it works ok now.
thanks guys
:thumbsup:
missing-score
05-05-2003, 02:26 PM
But surely the whole point is that you want letters WITHOUT spaces to be wrapped.
No the point was to have the line breaks, which the user entered when inputting the data to appear when being echoed out.
And for php to automatically insert a line break after 45 characters as well.
Its going to be a sentence so will have spaces and thats fine.
sorry if I confused you :p
:thumbsup:
missing-score
05-05-2003, 07:29 PM
So you want to count normal line breaks as well one every 45 chars, in the middle of words?
Why not just set:
<p align="justify"> and print it from there?
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.