Jump to content


Photo

nl2br


  • Please log in to reply
15 replies to this topic

#1 mafkeesxxx

mafkeesxxx
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 29 September 2006 - 08:18 AM

Hello!

I have made a form with a comment field. Now i want to apply the nl2br 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.


#2 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 29 September 2006 - 08:25 AM

nl2br should be used when you want to print the line out to view it.

#3 mafkeesxxx

mafkeesxxx
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 29 September 2006 - 09:00 AM

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>




#4 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 29 September 2006 - 09:03 AM

Here is a little example:

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

I don't need your script, I'll try to write it myself

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 29 September 2006 - 09:07 AM

Here is a little example:

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

WHy do people do that! I dont get it. Just do:
$mail = nl2br($_POST['mail']);


#6 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 29 September 2006 - 09:09 AM

Well you are right wildteen88 :)
I don't need your script, I'll try to write it myself

#7 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 29 September 2006 - 09:11 AM

wildteen88, why does it matter? It is personal preference. It may not be the most efficient way, but it works.

#8 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 29 September 2006 - 09:14 AM

and it's easy to understand for noob  ;)
I don't need your script, I'll try to write it myself

#9 mafkeesxxx

mafkeesxxx
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 29 September 2006 - 10:16 AM

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  :-\




#10 JasonLewis

JasonLewis
  • Members
  • PipPipPip
  • Advanced Member
  • 3,351 posts
  • LocationVictoria, Australia

Posted 29 September 2006 - 11:24 AM

ok. first off. what the others said is correct. {Comments} would be a variable.

so you would do:
$comments = nl2br($comments);

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

$comments = str_replace("\n", "<br>", $comments);

Good luck with your coding.
Jason / ProjectFear / Jaysonic

#11 mafkeesxxx

mafkeesxxx
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 29 September 2006 - 01:28 PM

Ok, still doens't work, this is my mailscript. Hope you can help me now!  :)




#12 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 29 September 2006 - 01:36 PM

nl2br($comments)

Should work? You don't need to make it a variable.


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

<?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";
?>

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#13 mafkeesxxx

mafkeesxxx
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 29 September 2006 - 01:48 PM

@HuggieBear

Thanks,

But i don't know how to apply it to the script i posted above.


#14 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 29 September 2006 - 01:55 PM

The function nl2br() does not change the argument, it returns the modified string, so a statement like:
<?php nl2br($str); ?>
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:
<?php echo "$string"; ?>
<?php echo $string; ?>

Ken

#15 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 29 September 2006 - 01:58 PM

Find this line in your code:
<?php
					$MailBody =  $TemplateData;
?>
replace it with:
<?php
					$MailBody =  nl2br($TemplateData);
?>

Ken

#16 mafkeesxxx

mafkeesxxx
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 29 September 2006 - 02:44 PM

Find this line in your code:

<?php
					$MailBody =  $TemplateData;
?>
replace it with:
<?php
					$MailBody =  nl2br($TemplateData);
?>

Ken


Thanks dude, Just what i needeed!

It works  ;D





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users