简体   繁体   English

如何用\\ n替换\\ n <br> 在PHP中

[英]How to REPLACE \n with <br> in PHP

I've tried nl2br() as well as ALL of the methods listed in the comments here . 我已经尝试过nl2br()以及此处评论中列出的所有方法。 The problem I have is that even though they do insert the <br/> , the \\n still remains in the text. 我的问题是,即使他们确实插入了<br/>\\n仍然保留在文本中。

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: 假设$row['body']"Hello\\nGoodbye" ,则html中的输出为:

Hello\n
Goodbye

I've tried using the method nl2br() directly with the string that is stored in the database, and it works correctly . 我尝试将nl2br()方法直接与存储在数据库中的字符串一起使用, 并且可以正常工作 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. 字符串以NVARCHAR2的形式存储在SQLite表中。

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." “三文治,三文治。Aliquamsapien nisl,sagittis id justo eget,placerat ultrices nisl。在临时书记中。 \\ n Cras mollis,在iaculis semper的小组成员,justo dui maximustellus,在quis dictum urna velit处坐着。 Suspendisse tristique augue at a gravgrav euismod。Ut tempus metus quis nisl sagittis volutpat。Nam rhoncus diam luctus dictum tristique。“

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. 如果您正在回显数据,并且在输出中看到一个实际的\\n ,那么您拥有的不是换行符,而是一个文字\\后面跟着n字符。 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). 然后,您应该在'\\n'上执行str_replace(请注意单引号,这样它就不会翻译成换行符)。

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. 这可能是要么使用的东西如addslashes(将一个隐蔽在插入由你做\\n换行符\\\\n )或可能由数据库的一些任意安全原因自动完成的,而不是翻译回。

只需使用str_replace将所有\\n替换为<br />

$tldr_clean = str_replace("\n", '<br />', $row['tldr']);
$body_clean = str_replace(
    ["\r", "\n"], 
    ['', '<br>'], 
    $row['body']
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM