如何在php中用`<BR>`替换所有新的字符串行? [英] How to replace all new lines of string with `<BR>` in php?

查看:43
本文介绍了如何在php中用`<BR>`替换所有新的字符串行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于\n 和\r 有太多的混淆.
关于\n 和\r 之间的差异,堆栈中有很多问题.简而言之,我的困惑/问题是:

There are too much confusion about \n and \r.
There are lot's of questions in stack about differences between \n and \r. In a nut shell My confusions/questions are:

  1. \r\n 是否等于 \n\r?
  2. \r\n 是否会导致两个新行?
  3. \r 是否会导致输出中出现新行?
  4. 如何在 PHP 中用 <BR> 标签替换所有新行?
  1. Is \r\n equal to \n\r?
  2. Is \r\n cause to two new lines?
  3. Is \r cause a new line in output?
  4. How can I replace all new lines with <BR> tag in PHP?

并非所有字符串都显式具有 \r 或 \n.假设我有一个 textarea 并且用户输入了一些字符,包括键盘的 Enter .未输入 \r 或 \n,但存在新行.如何删除所有新行?

Not all strings has \r or \n explicitly. Suppose I have a textarea and user inputs some characters including Enter of keyboard. No \r or \n is entered but new lines exist. How to remove all new lines?

推荐答案

微软视窗/MS-DOS:\r\n

Microsoft Windows / MS-DOS: \r\n

Acorn BBC 和 RISC OS 假脱机文本输出:\n\r

Acorn BBC and RISC OS spooled text output: \n\r

Apple Macintosh OS 9 及更早版本:\r

Apple Macintosh OS 9 and earlier: \r

Unix(例如 Linux),还有 Apple OS X 及更高版本:\n

Unix (e.g., Linux), also Apple OS X and higher: \n

替换 ->nl2br

更多相关信息这里

如果文本区域中存在新行而不是 \n、\r 或 \r\n!这些是不输出的控制字符.

If new lines exists in the textarea than \n,\r or \r\n are present! These are control characters which are not outputted.

要删除它们,您可以尝试:

To remove them you could try:

$string = str_replace(array("\r\n", "\n\r", "\r", "\n"), "", $string);

这篇关于如何在php中用`&lt;BR&gt;`替换所有新的字符串行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆