0

I'm printing out text from a database and I keep getting \r\n and I'm not completely sure why. This is how my code looks like

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>

I'm trying to use stripslashes but it's not working

I would like my text to be printed out like this

Test
Test

But instead it goes like this

Test\r\nTest 

EDIT

After following you guys everything seemed to go fine until I publish it. I think there's something wrong with my POST method

If anyone has the time to go over this that'd be greatly appreciated.

  <?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>
Valeh Hajiyev
  • 3,216
  • 4
  • 19
  • 28
IamGretar
  • 173
  • 2
  • 13
  • i'm worried about how you are checking your requests.. instead of your conditionals checking REQUEST_METHOD, use the $_POST global array. Also, you shouldn't stray away from HTTP. Use POST, and use a conditional in there. don't create another method named POST2 – ddavison Jun 17 '13 at 23:04
  • When I log in everything looks alright but when I update it with the post method everything goes back to the \r\n\ issue – IamGretar Jun 17 '13 at 23:29

3 Answers3

6

You need nl2br($_SESSION['bio'])

EDIT:

Since you should print the text inside <textarea>, try this one:

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

&#13; is Carriage return.

Valeh Hajiyev
  • 3,216
  • 4
  • 19
  • 28
1

stripslashes does not remove \r\n

You need: preg_replace('~\r?\n~','<br/>', $string);

transilvlad
  • 13,974
  • 13
  • 45
  • 80
0

This should do the trick:

<?php 
$string = "Hello\r\nWorld\r\n!\r\n"; 
$string = nl2br($string); 
echo $string;
?>

Good luck.

I recomend to give a look in to this post for more info about your problem.

Community
  • 1
  • 1
kcho0
  • 169
  • 1
  • 1
  • 7