简体   繁体   English

用&#39;替换&#39;\\ n&#39;最简单的方法是 <br> &#39;?

[英]What's the easiest way to replace '\n' with '<br>'?

I found some answers in the questions, but I didn't understand and AJAX was used in those solutions. 我在问题中找到了一些答案,但是我不明白,这些解决方案中使用了AJAX。

Now I want to find an easy solution to replace "Enter Key" or \\n with <br> to find new lines, in JavaScript or PHP. 现在,我想找到一个简单的解决方案,用JavaScript或PHP将<Enter>或\\ n替换为<br>以查找新行。

For a text input(visitors comments) in a <form> . 对于<form>的文本输入(访客评论)。

Is there any way to use string.search() and don't using AJAX or Jquery or some harder solutions? 有没有办法使用string.search()而不使用AJAX或Jquery或其他更难的解决方案?

PHP的内置函数nl2br()可以做到这一点。

nl2br是最简单的

<?php echo nl2br($_POST['someTextArea']); ?>

Use nl2br in PHP to change new lines to break tags. 在PHP中使用nl2br来更改换行符以破坏标签。 http://php.net/manual/en/function.nl2br.php http://php.net/manual/zh/function.nl2br.php

The easiest and most reliable way to do this is using PHP's built in nl2br() function. 最简单,最可靠的方法是使用PHP内置的nl2br()函数。

http://php.net/manual/en/function.nl2br.php http://php.net/manual/zh/function.nl2br.php

However, it looks like you would like to achieve this using javascript. 但是,您似乎希望使用javascript实现此目的。 That can be done using this: 可以使用以下方法完成:

value.replace(/\n/g,'<br />')

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

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