简体   繁体   English

从输出中删除\\ r \\ n

[英]remove \r\n from output

I know this question has been asked before but the answers are not the solution to my problem. 我知道已经问过这个问题,但是答案并不是解决我的问题的方法。

When i post a text with this code: 当我用此代码发布文本时:

$info=html_entity_decode(mysql_real_escape_string($_POST['info']));

Like : 喜欢 :

fdsa

fdsa

fasf

and when i posted this text with antered and spaces it looks like this 当我在文本后面加上空格和空格时,它看起来像这样

<?php echo $info;?>

fdsa\r\nfdsa\r\nfasf\r\n

i try this nl2br($info) but still not working. 我尝试使用此nl2br($info)但仍无法正常工作。

What do I need to appear in the text in this way? 这样我需要在文本中显示什么?

fdsa

fdsa

fasf

Replace the \\r\\n by <br> , the \\n by <br> , then the \\r by <br> : \\r\\n替换为<br> ,将\\n替换为<br> ,然后将\\r替换为<br>

$info = html_entity_decode($_POST['info']); 
$info = str_replace('\r\n' , '<br>', $info);
$info = str_replace('\n' , '<br>', $info);
$info = str_replace('\r' , '<br>', $info);

$info = html_entities($info);
echo $info;

You have to make multiples replacements since new lines can be represented differently according to the operating system (See this page for more details) 您必须进行多次替换,因为新行可能会根据操作系统而有所不同(请参阅此页面以获取更多详细信息)

Finally, sanitize the value with html_entities before echoing it, preventing client side attacks. 最后,在回显值之前用html_entities值,以防止客户端攻击。

EDIT : Removed the mysql_... function, not needed (the value isn't intended to be inserted in a MySQL database, not now at least). 编辑:删除了mysql _...函数,不需要(该值不打算插入MySQL数据库,至少现在不行)。

Also, read Lashus advice bellow and apply it ;) 另外,请阅读下面的Lashus建议书并将其应用;)

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

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