Hot to convert text to html

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Hot to convert text to html

Post by swetha »

Hi,
im trying to convert text to html.
i used nl2br function,but this function only adds a new line
to the text whenever it encounters "/n":

i.e if the string is "abc\ndef"
and i give echo nl2br(abc\ndef"),then i get the following output:
abc
def

i need to get the following output:
abc<br>
def

how do i get this output?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Hot to convert text to html

Post by onion2k »

Are you aware that...

Code: Select all

abc<br>
def
in HTML is actually...

Code: Select all

abc<br><br>
def
Run the text through nl2br() once, then through htmlentities(), and then put it through nl2br() a second time. Display that and you'll see the <br> tags (although they may actually be <br /> depending on which version of PHP you're running).
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: Hot to convert text to html

Post by swetha »

Code: Select all

 
$temp="abc\ndef";
htmlentities($temp);
echo nl2br($temp);
 
tied the above .stilll i get the same output.
<br/> is not getting displayed.
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: Hot to convert text to html

Post by swetha »

Code: Select all

 
<?php
$temp="abc\ndef";
nl2br($temp);
htmlentities($temp);
echo nl2br($temp);
?>
 
Hi,the above is what i did,but this doesnt seem to work.i still get the same output
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Hot to convert text to html

Post by papa »

Try:

Code: Select all

$temp= nl2br("abc\ndef");
echo htmlentities($temp);
 
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Hot to convert text to html

Post by onion2k »

swetha wrote:

Code: Select all

 
<?php
$temp="abc\ndef";
nl2br($temp);
htmlentities($temp);
echo nl2br($temp);
?>
 
Hi,the above is what i did,but this doesnt seem to work.i still get the same output
You aren't assigning the values returned from the functions to $temp.
Post Reply