摆脱\ r \ n

时间:2013-06-17 22:47:23

标签: php textarea newline

我打印出来自数据库的文字,我一直在 \ r \ n ,我不完全确定原因。这就是我的代码的样子

form action="portal.php" method="post" class="label-top">
    <div>
       <label for="name">News Message</span></label>
        <form method="post" action="" name="bio">
         <textarea name="bio" cols="25" rows="5">


             <?php echo stripslashes($_SESSION['bio']) ?> 


     </textarea><br>

我试图使用striplashes但它不能正常工作

我希望我的文字能像这样打印出来

Test
Test

但是它就像这样

Test\r\nTest 

修改

跟随你们之后,一切似乎都很好,直到我发布它。 我觉得我的POST方法有问题

如果有人有时间重复这一点,我们将非常感激。

  <?php
include("mysql_connect.php");
session_start();


if( $_SESSION['login'] != 1 ) {
header( 'Location:login.php' ) ;}


if($_SERVER['REQUEST_METHOD'] == 'POST') {
$bio = mysql_real_escape_string($_POST['bio']);
$user_input = mysql_query("UPDATE members SET bio ='$bio' WHERE username='$_SESSION[username]' ");
$_SESSION['bio'] = $bio;
    } 
    if($_SERVER['REQUEST_METHOD'] == 'POST2') {
$bio = mysql_real_escape_string($_POST['notification']);
$user_input = mysql_query("UPDATE members SET notification ='$notification' WHERE username='$_SESSION[username]' ");
$_SESSION['notification'] = $notifiation;
    } 

?>


<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;">
        <title>APPSTARME</title>
        <link href="css/style.css" rel="stylesheet" type="text/css" />
    </head>
<body>
    <header>
        <div class="logo">
            <a href="login.php">
                <img src="images/logo.png" alt="APPSTARME" />
            </a>
        </div>
        <div class="clear"></div>
    </header>
    <div class="content">
    <article>

<h2 class="underline">Logged in as  <?php echo ucfirst($_SESSION['username']); ?>  </span></h2>

            <form action="portal.php" method="post" class="label-top">
                <div>
                     <label for="name">News Message</span></label>
                     <form method="post" action="" name="bio">
<textarea name="bio" cols="25" rows="5">

<?php
 echo nl2br($_SESSION['bio']); ?> 
</textarea><br>
<input type="submit" value="Update Message" /></form>
                </div>

3 个答案:

答案 0 :(得分:6)

您需要nl2br($_SESSION['bio'])

修改

由于您应该在<textarea>内打印文本,请尝试以下方法:

preg_replace("/\r\n|\r|\n/",'&#13;',$_SESSION['bio']')

&#13;Carriage return

答案 1 :(得分:1)

stripslashes不会删除\r\n

您需要:preg_replace('~\r?\n~','<br/>', $string);

答案 2 :(得分:0)

这应该可以解决问题:

<?php 
$string = "Hello\r\nWorld\r\n!\r\n"; 
$string = nl2br($string); 
echo $string;
?>
祝你好运。

我建议您查看this帖子,了解有关您问题的更多信息。