Welcome to WebmasterWorld Guest from 23.22.220.37

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

nl2br (What's wrong here)?

     
6:40 pm on Aug 5, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 15, 2002
posts:971
votes: 0


Hey-

I'm just trying to get the information typed into MyTextarea to break onto a new line in TextareaHandler.php. When submitted the text is all in one single line across the page giving me a horizontal scrollbar...I don't thing I'm mistyping anything here- Is the nl2br function correct?

form.html
_____________

<html>
<head>
<title></title>
</head>

<body>

<form action="TextareaHandler.php" method="post">
<textarea cols="40" rows="5" name="MyTextarea"></textarea>
<input type="submit" value="Send Form" />
</form>

</body>
</html>

TextareaHandler.php
_____________________

<?php
$MyTextarea = nl2br($MyTextarea);
echo($MyTextarea);
?>

-Thanks

[edited by: madcat at 7:18 pm (utc) on Aug. 5, 2002]

6:58 pm on Aug 5, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Feb 4, 2002
posts:5044
votes: 0


My guess would be that your not hitting return when filling out the textarea. So there are no newlines.

There's nothing wrong with your script other than the absence of any error checking?

Nick

7:14 pm on Aug 5, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 15, 2002
posts:971
votes: 0


Well, it's when I click on the submit button you know. It should work for sure, but I'm pretty new to this so my working around hold-ups is a bit limited.
7:38 pm on Aug 5, 2002 (gmt 0)

Preferred Member

10+ Year Member

joined:May 9, 2001
posts:416
votes: 0


If you want more control over it use the wordwrap() function first, then change the newlines to breaks with nl2br:

<?php
$MyTextarea = wordwrap($MyTextarea, 39);
$MyTextarea = nl2br($MyTextarea);
echo($MyTextarea);
?>

7:50 pm on Aug 5, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 15, 2002
posts:971
votes: 0


hmmm...thanks for your help. Now I'm convinced something else is going on though...I plugged in your code exactly and no breaks are added, then I tried this line:

$_REQUEST['MyTextarea'] = ereg_replace("(\r\nŠ\nŠ\r)", "<br />",$_REQUEST['MyTextarea']);

And I get an error on line 2. Oh yeah, also:

$MyTextarea = ereg_replace("(\r\nŠ\nŠ\r)", "<br />",$MyTextarea);

Same error-

Everything is spelled out correctly, so ?

9:39 pm on Aug 5, 2002 (gmt 0)

Preferred Member

10+ Year Member

joined:May 9, 2001
posts:416
votes: 0


Just to clarify - The form textarea will wrap your entry at the right edge of the box, but this doesn't result in newline characters in the returned string. Only if you use carriage return or type in a \n will newlines appear.

So, if you enter one long line in the textarea (with no carriage returns) that's what you'll get out of it, and the nl2br() function won't have any newlines to change to breaks.

That's where the wordwrap() function comes in: notice it's set to 39 characters. That's approximately the width of your textarea (cols="40") so it will emulate the text as it appears in the textarea box by wrapping at the 39th character.

No need for fancy eregs. :)

9:51 pm on Aug 5, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 15, 2002
posts:971
votes: 0


This is my .php file:

<?php
$MyTextarea = wordwrap($MyTextarea, 39);
$MyTextarea = nl2br($MyTextarea);
echo($MyTextarea);
?>

The problem is that anything I try won't wrap. I could be way off but I feel like I might have to change something in php.ini?

Between all of the examples presented above, there is no way that one of those shouldn't be working correctly.

Windows 2000 / Apache if that helps.

10:00 pm on Aug 5, 2002 (gmt 0)

Preferred Member

10+ Year Member

joined:May 9, 2001
posts:416
votes: 0


Are you using at least php 4.0.2?
10:36 pm on Aug 5, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 15, 2002
posts:971
votes: 0


Yeah, I'm using 4.1.1...This is really strange. Can I atleast eliminate that the problem is in the php.ini file?

Although I hope it's there cuz it isn't anywhere that I can see;)

10:56 pm on Aug 5, 2002 (gmt 0)

Preferred Member

10+ Year Member

joined:May 9, 2001
posts:416
votes: 0


> Can I at least eliminate that the problem is in the php.ini file?

Yes, I think that's safe.

Must be frustrating.

For the sake of coordination in this problem-solving venture copy this code:

<html>
<head>
<title></title>
</head>
<body>
<form action="TextareaHandler.php" method="post">
<textarea cols="40" rows="5" name="MyTextarea"></textarea>
<input type="submit" value="Send Form">
</form>
</body>
</html>
<?php
$MyTextarea = wordwrap($MyTextarea, 39);
$MyTextarea = nl2br($MyTextarea);
echo($MyTextarea);
?>

...save it as TextareaHandler.php, run it, then stuff this one long line into the form:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diem nonummy nibh euismod tincidunt ut lacreet dolore magna aliguam erat volutpat. Ut wisis enim ad minim veniam, quis nostrud exerci tution ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis te feugifacilisi.

...see if it wraps.

I just want to duplicate what I'm doing here.

11:23 pm on Aug 5, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 15, 2002
posts:971
votes: 0


Ok, now we're cookin' toadhall- thanks...I'm still very lost as to why this works and the other code didn't. I tried it both ways...by pasting it all in one file and splitting up the php and html files...breaks fine;)
11:45 pm on Aug 5, 2002 (gmt 0)

Preferred Member

10+ Year Member

joined:May 9, 2001
posts:416
votes: 0


Mystery. Perhaps a cache problem?

Ah well, who cares? It's working. :)