PHP nl2br() 函数

PHP 教程 · 2019-01-29 18:13:44

实例

在字符串中的新行(\n)之前插入换行符:

<?php echo nl2br("One line.\nAnother line."); ?>

上面代码的浏览器输出如下:

One line.
Another line.

上面代码的 HTML 输入如下(查看源代码):

One line.<br />
Another line.

定义和用法

nl2br() 函数在字符串中的每个新行(\n)之前插入 HTML 换行符(<br> 或 <br />)。

语法


nl2br(string,xhtml)


参数 描述
string 必需。规定要检查的字符串。
xhtml 可选。一个表示是否使用兼容 XHTML 换行的布尔值:
  • TRUE- 默认。插入 <br />
  • FALSE - 插入 <br>

技术细节

返回值: 返回已转换的字符串。
PHP 版本: 4+
更新日志: 在 PHP 4.0.5 之前,该函数插入 <br>。在 PHP 4.0.5 之后,该函数插入兼容 XHTML 的 <br />。

在 PHP 5.3 中,新增了 xhtml 参数。

更多实例

实例 1

通过使用 xhtml 参数,在新行(\n)之前插入换行符:

<?php echo nl2br("One line.\nAnother line.",false); ?>

上面代码的浏览器输出如下:

One line.
Another line.

上面代码的 HTML 输入如下(查看源代码):

One line.<br>
Another line.

点击查看所有 PHP 教程 文章: https://codercto.com/courses/l/5.html

查看所有标签

The Hard Thing About Hard Things

The Hard Thing About Hard Things

Ben Horowitz / HarperBusiness / 2014-3-4 / USD 29.99

Ben Horowitz, cofounder of Andreessen Horowitz and one of Silicon Valley's most respected and experienced entrepreneurs, offers essential advice on building and running a startup—practical wisdom for ......一起来看看 《The Hard Thing About Hard Things》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具