导航首页 » 技术教程 » PHP字符串函数nl2br()的用法
PHP字符串函数nl2br()的用法 144 2023-12-12   

nl2br

(PHP 4, PHP 5)

nl2br — 在字符串所有新行之前插入 HTML 换行标记

说明

string nl2br ( string $string [, bool $is_xhtml = true ] )

在字符串 string 所有新行之前插入 '<br />' 或 '<br>',并返回。

参数

string

输入字符串。

is_xhtml

是否使用 XHTML 兼容换行符。

返回值

返回调整后的字符串。

范例

Example #1 nl2br() 使用范例

<?php
echo nl2br("foo isn'tn bar");
?>

以上例程会输出:

foo isn't<br />
 bar

Example #2 使用 is_xhtml 生成合法的 HTML 标记

<?php
echo nl2br("WelcomernThis is my HTML document", false);
?>

以上例程会输出:

Welcome<br>
This is my HTML document

Example #3 各种换行分隔符

<?php
$string = "Thisrnisnranstringr";
echo nl2br($string);
?>

以上例程会输出:

This<br />
is<br />
a<br />
string<br />

更新日志

版本 说明 5.3.0 新增可选的 is_xhtml 参数。 4.0.5 nl2br() 成为 XHTML 兼容的操作。所有的旧版本都会在 string 的新行之前插入 '<br>' 而不是 '<br />'。

参见

htmlspecialchars() - Convert special characters to HTML entities htmlentities() - Convert all applicable characters to HTML entities wordwrap() - 打断字符串为指定数量的字串 str_replace() - 子字符串替换


!!!站长长期在线接!!!

网站、小程序:定制开发/二次开发/仿制开发等

各种疑难杂症解决/定制接口/定制采集等

站长微信:lxwl520520

站长QQ:1737366103