How do I get the text look like I typed it in...

General discussions related to php

Moderators: macek, egami, gesf

Mazerman
New php-forum User
New php-forum User
Posts: 12
Joined: Sun Jan 26, 2003 2:57 pm
Location: Sweden

How do I get the text look like I typed it in...

Postby Mazerman » Tue Jul 22, 2003 8:10 pm

Hi!

I wonder how I can get the text look like I typed it, in a textarea when I send it. I mean if I type this text in a textarea and then send it with a form:

"Live: Man Utd 2-0 Celtic
Stewart to quit England
Pizzonia in Jaguar snub"


It will look like this when I print it on another page, ie it will have removed all the new lines:

"Live: Man Utd 2-0 Celtic Stewart to quit England Pizzonia in Jaguar snub"

I use this code:

--site1--

Code: Select all

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="form1" ENCTYPE="multipart/form-data" action="test2.php" method="post">
  <textarea name="textarea" cols="50" rows="10"></textarea>
  <input name="submit" type="submit" value="send">
</form>
</body>
</html>


---site2---

Code: Select all

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
   $txt = htmlspecialchars($_POST['textarea']);
   echo $txt;   
?>
</body>
</html>


Would be thankful for an answear...

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Tue Jul 22, 2003 9:07 pm

Browsers ignore newlines when reading HTML. Use nl2br() to convert newlines to HTML line breaks ("<br />").

User avatar
mike
New php-forum User
New php-forum User
Posts: 73
Joined: Sun May 04, 2003 4:26 am
Location: Athens
Contact:

Postby mike » Sat Jul 26, 2003 3:57 am

swirlee wrote:Browsers ignore newlines when reading HTML. Use nl2br() to convert newlines to HTML line breaks ("<br />").


Its better to use str_replace("\n","<br>",$text) instead of nl2br. Dont remember exactly why... but nl2br has some minor problems

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Sat Jul 26, 2003 12:06 pm

I can't imagine why this would be (the comments in the documentation don't suggest anything), but if you're going to use str_replace(), you shouldn't forget the newline: str_replace("\n","<br>\n",$text). Without the newline, you'll end up with your entire block of text on a single line in the HTML (though it'll display correctly), which is fugly.

User avatar
mike
New php-forum User
New php-forum User
Posts: 73
Joined: Sun May 04, 2003 4:26 am
Location: Athens
Contact:

Postby mike » Sun Jul 27, 2003 3:07 am

you shouldn't forget the newline: str_replace("\n","<br>\n",$text).


This is useless... just replace \n with <br>. There is a post somewhere on this forum I m explaining what nl2br cant do and str_place can. Dont remember what was that...

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Sun Jul 27, 2003 4:12 pm

If I have a bunch of newline-separated text and want to convert it to <br>-separated text, I sure as hell don't want the whole block of HTML to be one long, ugly line like this:

Code: Select all

This is useless... just replace<br>\n with &br&. There is a post<br>somewhere on this forum I m explaining<br>what nl2br cant do and str_place can.<br>Dont remember what was that...


You wouldn't write your HTML like that, and neither should your code. It's just sloppy. There's a reason newlines were invented.

User avatar
mike
New php-forum User
New php-forum User
Posts: 73
Joined: Sun May 04, 2003 4:26 am
Location: Athens
Contact:

Postby mike » Mon Jul 28, 2003 1:31 am

Well I used both ways many times and I prefer str_replace... for many reasons. Everyone has his own way :wink:


Return to “PHP General”

Who is online

Users browsing this forum: No registered users and 1 guest

cron