1

I've tried nl2br() as well as ALL of the methods listed in the comments here. The problem I have is that even though they do insert the <br/>, the \n still remains in the text.

This is what I was originally doing:

<?php

$stmt = $db->prepare(...)

$stmt->execute();

while ($row = $stmt->fetch()) {

    $tldr_clean = nl2br($row['tldr']);
    $body_clean = nl2br($row['body']);

    ?>

    <section>

        <h2 class="tldr"><?= $tldr_clean ?> <?= $row['score']?>/10</h2>

        <p class="body"><?= $body_clean ?></p>

        <p class="reviewer">Written by <?= $row['name'] ?></p>

    </section>

<?php
} ?>

Assuming $row['body'] is "Hello\nGoodbye", the output in the html is:

Hello\n
Goodbye

I've tried using the method nl2br() directly with the string that is stored in the database, and it works correctly. So I'm guessing the problem is in calling the method with a variable? How can I fix it?

Edit:

String is stored as NVARCHAR2 in an SQLite table.

String stored in database:

"Fusce vitae purus tristique, efficitur dolor et, tristique ante. Aliquam sapien nisl, sagittis id justo eget, placerat ultrices nisl. In tempus sollicitudin mauris ut feugiat. Pellentesque imperdiet risus at ex dictum, sed tempus est pulvinar. Nunc nec leo fringilla diam sodales euismod sed sed odio.\n Cras mollis, quam at iaculis semper, justo dui maximus tellus, quis dictum urna velit sit amet justo. Curabitur consectetur fringilla arcu, sit amet ultrices est dignissim eget. Etiam non dapibus justo, et semper quam. Suspendisse tristique augue sit amet gravida euismod. Ut tempus metus quis nisl sagittis volutpat. Nam rhoncus diam luctus dictum tristique."

Tirafesi
  • 1,297
  • 2
  • 18
  • 36
  • 6
    You must have a literal '\n' in there then. Check your inserted value to make sure you're actually inserting a line break and not a backslash and n. – Devon Bessemer Dec 14 '16 at 19:13
  • 2
    The documentation says `Returns string with
    or
    inserted before all newlines (\r\n, \n\r, \n and \r).` So it is the expected behavior
    – J. Pichardo Dec 14 '16 at 19:14
  • Look at the accepted answer here: http://stackoverflow.com/a/8627926/5530965 Perhaps it is saved in the database as HTML entities? – dokgu Dec 14 '16 at 19:17
  • Your `\n` is stored in the database as an escaped string. Can you show your code in how you saved this in the database? – dokgu Dec 14 '16 at 19:27
  • 1
    If you are echo'ing the data out and you see an actual `\n` in the output, then what you have isn't not a newline character, but a literal `\ ` followed by `n` character. Usually these are translated to a newline if in code, but if in string in a variable (such as when pulled from a database), it isn't. You should then do a str_replace on `'\n'` (notice the single quotes so it isn't translated to a newline character). – Jonathan Kuhn Dec 14 '16 at 19:27
  • @uom-pgregorio See the edit I made. The string is stored as NVARCHAR2 in SQLite database. (this string is part of the database by default, it wasn't inserted through php) – Tirafesi Dec 14 '16 at 19:31
  • @JonathanKuhn It worked! Thank you so much :D like you said, the single quotes on the str_replace made the difference. Wanna write an answer so I' accept it? :b – Tirafesi Dec 14 '16 at 19:34

3 Answers3

5

If you are echo'ing the data out and you see an actual \n in the output, then what you have isn't not a newline character, but a literal \ followed by n character. Usually these are translated to a newline if in code, but if in string in a variable (such as when pulled from a database), it isn't. You should then do a str_replace on '\n' (notice the single quotes so it isn't translated to a newline character).

This is likely either done by you on insert using something like addslashes (would covert a \n newline to \\n) or possibly done automatically by the database for some arbitrary security reason and not translated back.

Jonathan Kuhn
  • 15,279
  • 3
  • 32
  • 43
4

Just use str_replace to replace all \n's with <br />:

$tldr_clean = str_replace("\n", '<br />', $row['tldr']);
Marco
  • 7,007
  • 2
  • 19
  • 49
0
$body_clean = str_replace(
    ["\r", "\n"], 
    ['', '<br>'], 
    $row['body']
);
Leonid Zakharov
  • 940
  • 1
  • 6
  • 11