Retaining Paragraphs and Spaces on my database

Started on the old forum in July 2012 · view the original in the archive · old links redirect here

8 replies · 600 views · started 14 years ago · 1 min read

5 participants
triplee02 Newbie · 1 posts #1

Am currently designing a website for a company and the problem i gat is dat the form i created for the employees. saves all letters and alpabets on one line. the enter key does not space the information. i hope somebody understand me. for example if i type: hello pls kal

mr john.

it will save as hello pls kal mr john.. without the break line.

pls help

ReazulIqbal Developer · 103 posts #2

Use

<textarea></textarea>

in place of

<input type="text" />

to get text like that

GumSlone Administrator Principal · 1,690 posts #3

is there some filter which replaces line breaks before it adds the text to the database? something similar like:

$str = str_replace(array("\r\n", "\n", "\r"), '', $str);

if not, to show line breaks from database the same way in html you have to use a code like this:

$str = str_replace(array("\r\n", "\n", "\r"), '<br/>', $str);

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

Ghost Senior · 366 posts #4

GumSlone wrote: is there some filter which replaces line breaks before it adds the text to the database? something similar like:

$str = str_replace(array("\r\n", "\n", "\r"), '', $str);

if not, to show line breaks from database the same way in html you have to use a code like this:

$str = str_replace(array("\r\n", "\n", "\r"), '<br/>', $str);

just a question but wouldnt it be just as easy to do something like:

$post = nl2br($post);
//
//
//

??

obviously $post var = their own post variable.

as far as i know this does basically same thing. turning new lines into also.

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
GumSlone Administrator Principal · 1,690 posts #5

Ghost wrote: just a question but wouldnt it be just as easy to do something like:

$post = nl2br($post);
//
//
//

??

obviously $post var = their own post variable.

as far as i know this does basically same thing. turning new lines into also.

yes, you are right your solution is better, this function was made specially to insert html line break into the text.

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

Ghost Senior · 366 posts #6

GumSlone wrote: yes, you are right your solution is better, this function was made specially to insert html line break into the text.

came up with a really nice solution to a flat file writing problem when using nl2br by adding it to a modded version of gumslones.

using nl2br with txt files makes your text look like this inside the .txt file:

this is

a trial message

that im producing to

use as an example.

taking up 4 lines in the .txt file. (when trying to show to users it may look like its coming from 1 post, but it takes up a count of 4 posts in total)

by modding gumslones code and using nl2br you can produce this inside the .txt file:

this is a trial message that im producing to use as an example.

ramming everything on 1 line and once again counting as 1 post total. so the display structure is always correct.

heres what i did.

/** Call Cleanse Function And <br /> All \r\n \r \n With nl2br */
$Post = nl2br(Cleanse($_POST['post']));
/** Remove \r\n \r \n From <br /> Tags That nl2br Seem To Leave Behind. */
$Post = str_replace(array("<br />\r\n", "<br />\n", "<br />\r"), '<br/>', $Post); 
//
//
//

Cleanse() is just a function i got for cleaning the posts also.

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
arnage Lead · 811 posts #7

You can use php end of line, it remains \n in markup but must be in double quotes. If you don't want it change those in single quotes. Btw i don't like nl2br because its white space, i like not . 🙂

str_replace(PHP_EOL, "<br/>", $var);

or...

$Post = str_replace(PHP_EOL, "<br/>", Cleanse($_POST['post']));
<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
Ghost Senior · 366 posts #8

arnage wrote: You can use php end of line, it remains \n in markup but must be in double quotes. If you don't want it change those in single quotes. Btw i don't like nl2br because its white space, i like not . 🙂

str_replace(PHP_EOL, "<br/>", $var);

or...

$Post = str_replace(PHP_EOL, "<br/>", Cleanse($_POST['post']));

thats why i added the:

$Post = str_replace(array("<br />\r\n", "<br />\n", "<br />\r"), '<br/>', $Post); 
//
//
//

after using nl2br, not only does it remove the hidden new lines it leaves next to the

tags but it also turns into and cleans it up 🙂

thanks for the input though 🙂

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
arnage Lead · 811 posts #9

Np. Cool. 🙂

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close