nl2br

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't  bar");
?>

以上例程会输出:

foo isn't<br />
 bar

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

<?php
echo nl2br("Welcome This is my HTML document"false);
?>

以上例程会输出:

Welcome<br>
This is my HTML document

更新日志

版本 说明
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() - 子字符串替换

  • addcslashes
  • addslashes
  • bin2hex
  • chop
  • chr
  • chunk_split
  • convert_cyr_string
  • convert_uudecode
  • convert_uuencode
  • count_chars
  • crc32
  • crypt
  • echo
  • explode
  • fprintf
  • get_html_translation_table
  • hebrev
  • hebrevc
  • hex2bin
  • htmlentities
  • htmlspecialchars
  • htmlspecialchars_decode
  • html_entity_decode
  • implode
  • join
  • levenshtein
  • localeconv
  • ltrim
  • md5
  • md5_file
  • metaphone
  • money_format
  • nl2br
  • nl_langinfo
  • number_format
  • ord
  • parse_str
  • print
  • printf
  • quoted_printable_decode
  • quoted_printable_encode
  • quotemeta
  • rtrim
  • setlocale
  • sha1
  • sha1_file
  • similar_text
  • soundex
  • sprintf
  • sscanf
  • strcasecmp
  • strchr
  • strcmp
  • strcoll
  • strcspn
  • stripcslashes
  • stripos
  • stripslashes
  • strip_tags
  • stristr
  • strlen
  • strnatcasecmp
  • strnatcmp
  • strncasecmp
  • strncmp
  • strpbrk
  • strpos
  • strrchr
  • strrev
  • strripos
  • strrpos
  • strspn
  • strstr
  • strtok
  • strtolower
  • strtoupper
  • strtr
  • str_getcsv
  • str_ireplace
  • str_pad
  • str_repeat
  • str_replace
  • str_rot13
  • str_shuffle
  • str_split
  • str_word_count
  • substr
  • substr_compare
  • substr_count
  • substr_replace
  • trim
  • ucfirst
  • ucwords
  • vfprintf
  • vprintf
  • vsprintf
  • wordwrap
  • PHP MySQL HTML CSS JavaScript MSSQL AJAX .NET JSP Linux Mac ASP 服务器 SQL jQuery C# C++ java Android IOS oracle MongoDB SQLite wamp 交通频道