PDA

View Full Version : Line "Breaks" with html and php


Phocus
Mon 13th Aug '01, 9:40am
I couldnt think of a better subject for this post, so i tried my best. I also ran a few searches looking for something on this topic but I couldnt find anything.

I've never been able to get "line breaks" when im using a form to work, for example, if i enter:

line one blah balh

line 2 blah blah

Itll echo like:

line one blah balh line 2 blah blah

Is there something i can do through PHP and/or HTML that can get around this? Cuz its been a probolem thats been bugging me whenever i work with forms and sql querys and itll make it simpler than entering <br> tags every time i want to break lines.

Thanx a bunch!
~Mike

Mark Hensler
Mon 13th Aug '01, 11:01am
If you echo "\n";, you'll have line breaks when you view source.

echo "line one blah balh\n";
echo "line 2 blah blah\n";

krs-one
Mon 13th Aug '01, 11:27am
are you also talking about when you output text to the browser? in that case, a "\n" doesn't work, and you have to use the function nl2br() like so:


$text = nl2br($oldtext);
print $text;


Hope this helps.

-Vic

Phocus
Mon 13th Aug '01, 11:27am
EDIT: Ok i wrote this after i saw the first reply, and got the second reply while i was writing it, sorry

The thing is, im working with forms, so if i do like

<textarea name='testers' rows='20'></textarea>

and then submit it to a database, or send it to another page, and like the text area would have

line 01

line01

And if i did echoed $testers

or if i pulled the row from a database Id get

line01 line02

And i wana find out if theres a way to have php or html show the lne breaks, without me havnt to manually insert \n characters or <br> tags

Mark Hensler
Mon 13th Aug '01, 11:36am
Is nl2br() what you were looking for?

You could also use <pre> or <code> HTML tags.

You could also throw this at the mySQL server..
SELECT replace(field_name,"\n","<BR>\n") FROM table_name

Phocus
Mon 13th Aug '01, 11:45am
WOoO HOOOO!!

The <pre></pre> tags do just the thing, so i dont have to enter any <br> tags or \n characters.


WOOHOOO!!

thanx a bunch

krs-one
Mon 13th Aug '01, 12:19pm
in case anyone decided not to read my post, i suggested using the nl2br function.

that, along with PRE tags are not XHTML standards compliant, while the nl2br function attempts to maintain XHTML standards.

but, you will not always be able to use PRE tags to get what you want done, so, just to be safe, use nl2br.

-vic

Mark Hensler
Mon 13th Aug '01, 3:54pm
<pre> tags aren't XHTML standard?
Are the <code> tags?

Phocus
Mon 13th Aug '01, 4:52pm
Im sorry, i thought when you were talking about the nl2br() function, it would change the \n characters to <br> tags (nl 2 br, New Line too Break tag i kinda thought it ment) so i didnt try it, cuz i didnt want to have to enter \n characters

BUT it worked, and the <pre> tags turned out to not work as i would have hoped, because it woudlnt auto return lines and stuff



thank you