Jump to content

Archived

This topic is now archived and is closed to further replies.

mafkeesxxx

nl2br

Recommended Posts

Hello!

I have made a form with a comment field. Now i want to apply the [b]nl2br[/b] string to my comment field. I've tried some things, but didn't work. How can i do this?

This is my HTML:

<textarea name="Comments" cols="60" rows="15"></textarea>

Thanks in advance.

Share this post


Link to post
Share on other sites
The comment field will be send via e-mail.

So i want to apply nl2br to the field, otherwise i recieve the mailed comment field without enters <br>


Share this post


Link to post
Share on other sites
Here is a little example:

[code]<?php
$var = $_POST['mail']; // something from form
$mail = nl2br($var); // add nl2br to it
?>[/code]

Share this post


Link to post
Share on other sites
[quote author=Gruzin link=topic=109934.msg443556#msg443556 date=1159520582]
Here is a little example:

[code]<?php
$var = $_POST['mail']; // something from form
$mail =  // add nl2br to it
?>[/code]
[/quote]
WHy do people do that! I dont get it. Just do:
[code=php:0]$mail = nl2br($_POST['mail']);[/code]

Share this post


Link to post
Share on other sites
wildteen88, why does it matter? It is personal preference. It may not be the most efficient way, but it works.

Share this post


Link to post
Share on other sites
Ok, Thanks for all your help, but probably is a bit more complicated.
I have a mail form wich is HTML with mail.php included.

This results in a HTML template that is send via mail and recognises the form fields like this {Comments}

Now i want to apply the nl2br to the {Comments}

Damn, LOL  ;D ???

Hope you understand  :-\


Share this post


Link to post
Share on other sites
ok. first off. what the others said is correct. {Comments} would be a variable.

so you would do:
[code]$comments = nl2br($comments);[/code]

or you could do this, but its easier to do the above:

[code]$comments = str_replace("\n", "<br>", $comments);[/code]

Share this post


Link to post
Share on other sites
[quote author=Mutley]
[code]nl2br($comments)[/code]

Should work? You don't need to make it a variable.
[/quote]

I don't know what version of PHP you're using, but mine requires you to assign it to a variable, unless you're echoing it directly :D

[code]
<?php
$comments = <<<EOT
This is line one
This is line two
This is line three
EOT;

// This will print on one line
echo "$comments";

// This will print on one line
nl2br($comments);
echo "$comments";

// This will print on multiple lines
echo nl2br($comments);

// This will print on multiple lines
$comments = nl2br($comments);
echo "$comments";
?>
[/code]

Regards
Huggie

Share this post


Link to post
Share on other sites
The function nl2br() does not change the argument, it returns the modified string, so a statement like:
[code]<?php nl2br($str); ?>[/code]
by itself does not do anything. You need to either echo the result or assign the result to a variable.

Also, using double quotes around one variable when you're doing an echo are not needed. The following two statements will produce identical output:
[code]<?php echo "$string"; ?>[/code]
[code]<?php echo $string; ?>[/code]

Ken

Share this post


Link to post
Share on other sites
Find this line in your code:
[code]<?php
$MailBody =  $TemplateData;
?>[/code]
replace it with:
[code]<?php
$MailBody =  nl2br($TemplateData);
?>[/code]

Ken

Share this post


Link to post
Share on other sites
[quote author=kenrbnsn link=topic=109934.msg443693#msg443693 date=1159538318]
Find this line in your code:
[code]<?php
$MailBody =  $TemplateData;
?>[/code]
replace it with:
[code]<?php
$MailBody =  nl2br($TemplateData);
?>[/code]

Ken
[/quote]

Thanks dude, Just what i needeed!

It works  ;D

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.