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://www.codercto.com/courses/l/5.html

查看所有标签

PHP and MySQL Web Development (3rd Edition) (Developer's Library

PHP and MySQL Web Development (3rd Edition) (Developer's Library

Luke Welling、Laura Thomson / Sams / 2004-09-29 / USD 49.99

We've taken the best and made it even better. The third edition of the best-selling PHP and MySQL Web Development has been updated to include material and code on MySQL 5, PHP 5 and on PHPs object mod......一起来看看 《PHP and MySQL Web Development (3rd Edition) (Developer's Library》 这本书的介绍吧!

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

在线压缩/解压 CSS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具