如何在不使用html标签的情况下使用换行符?

时间:2013-05-06 10:08:50

标签: php

这是我的代码:

$blogid = mysql_real_escape_string($_GET['id']); 

if((isset($_POST['comment']))&&(!(trim($_POST['comment'])==FALSE))&&(isset($_SESSION['userid']))){

   $comment = mysql_real_escape_string($_POST['comment']); 
   $querycomment = "INSERT INTO `comment` (`userid`, `blogid`, `body`) VALUES ( '".$_SESSION['userid']."', '".$blogid."', '".$comment."');";
   $rowchat = mysql_query($querymess,$db_con) or die("Failed: " . mysql_error() );

}

<form method="post" action="blog.php?id=<?php echo $blogid; ?>" >
<textarea name="comment" ></textarea>
<input type="submit" value="send" name="submit" />
</form>

当用户评论此内容时:

This
is
my
world

然后在评论列表中出现:

This is my world

为什么换行不起作用?

1 个答案:

答案 0 :(得分:5)

因为换行符保存为\n

在回显字符串之前,您可以使用PHP函数nl2br($string)

<?php
    $string = "This\nis\nmy\nworld";
    echo nl2br($string);
?>
相关问题