PHP nl2br 基本語法
nl2br( $string )
語法小括號中的字串 $string 為必填項目,就是要轉換的字串,nl2br 可以判斷的換行符號如 \r\n、\n\r、\n 與 \r,遇到字串中有這些符號的時候,nl2br 便會將其轉換為 <br>、<br/> 或 </br> 標籤,並返回轉換結果。
PHP nl2br 範例
<?php
$new_string = "Welcome to Wibibi.\nIt was a very nice day.";
echo nl2br($new_string);
?>
以上輸出網頁結果$new_string = "Welcome to Wibibi.\nIt was a very nice day.";
echo nl2br($new_string);
?>
Welcome to Wibibi.
It was a very nice day.
檢視程式碼It was a very nice day.
Welcome to Wibibi.<br />
It was a very nice day.
範例的字串中有一個 \n 的換行標籤,這個標籤瀏覽器無法判斷後顯示換行,所以我們用了 nl2br 去轉換,輸出結果確實產生了換行的效果,檢視原始碼即可看到多出了一個 <br /> 的 HTML 換行 標籤。It was a very nice day.
PHP nl2br 相關主題研究