homepage Welcome to WebmasterWorld Guest from 54.226.10.234
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
nl2br (What's wrong here)?
madcat

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 521 posted 6:40 pm on Aug 5, 2002 (gmt 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]

 

Nick_W

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



 
Msg#: 521 posted 6:58 pm on Aug 5, 2002 (gmt 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

madcat

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 521 posted 7:14 pm on Aug 5, 2002 (gmt 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.

toadhall

10+ Year Member



 
Msg#: 521 posted 7:38 pm on Aug 5, 2002 (gmt 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);
?>

madcat

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 521 posted 7:50 pm on Aug 5, 2002 (gmt 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 ?

toadhall

10+ Year Member



 
Msg#: 521 posted 9:39 pm on Aug 5, 2002 (gmt 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. :)

madcat

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 521 posted 9:51 pm on Aug 5, 2002 (gmt 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.

toadhall

10+ Year Member



 
Msg#: 521 posted 10:00 pm on Aug 5, 2002 (gmt 0)

Are you using at least php 4.0.2?

madcat

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 521 posted 10:36 pm on Aug 5, 2002 (gmt 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;)

toadhall

10+ Year Member



 
Msg#: 521 posted 10:56 pm on Aug 5, 2002 (gmt 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.

madcat

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 521 posted 11:23 pm on Aug 5, 2002 (gmt 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;)

toadhall

10+ Year Member



 
Msg#: 521 posted 11:45 pm on Aug 5, 2002 (gmt 0)

Mystery. Perhaps a cache problem?

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved