PHP獲取表單textarea數(shù)據(jù)中的換行問題
更新時間:2010年09月10日 21:21:06 作者:
閑來無事,在網(wǎng)上看到一篇關(guān)于php表單轉(zhuǎn)換textarea換行符的文章,看完后,根據(jù)以往經(jīng)驗,感覺上這篇文章中的一些信息并不準(zhǔn)確...于是便自己親自對php獲取表單數(shù)據(jù)中的換行符問題進(jìn)行研究
測試頁面代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP獲取表單area數(shù)據(jù)中的換行問題</title>
</head>
<body>
<?php
$content=empty($_POST['content'])?null:trim($_POST['content']);
if(!empty($content))echo str_replace("\r",'rl',nl2br($content));
echo "\r".'<br/>----------分割線----------------------'."\r";
if(!empty($content))echo str_replace("\n",'nl',nl2br($content));
echo "\n".'<br/>----------分割線----------------------'."\n";
if(!empty($content))echo str_replace("\r",'rl',str_replace("\n",'nl',nl2br($content)));
echo "\r".'<br/>----------分割線----------------------<br/>'."\n";
echo 'hello'."\n".'boys!';
echo 'hello'."\r".'boys!';
?>
<form action="textareanl.php" method="post" enctype="multipart/form-data">
<textarea name="content" cols="20" rows="6"></textarea>
<br />
<input type="submit" value="提交" />
</form>
</body>
</html>
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP獲取表單area數(shù)據(jù)中的換行問題</title>
</head>
<body>
<?php
$content=empty($_POST['content'])?null:trim($_POST['content']);
if(!empty($content))echo str_replace("\r",'rl',nl2br($content));
echo "\r".'<br/>----------分割線----------------------'."\r";
if(!empty($content))echo str_replace("\n",'nl',nl2br($content));
echo "\n".'<br/>----------分割線----------------------'."\n";
if(!empty($content))echo str_replace("\r",'rl',str_replace("\n",'nl',nl2br($content)));
echo "\r".'<br/>----------分割線----------------------<br/>'."\n";
echo 'hello'."\n".'boys!';
echo 'hello'."\r".'boys!';
?>
<form action="textareanl.php" method="post" enctype="multipart/form-data">
<textarea name="content" cols="20" rows="6"></textarea>
<br />
<input type="submit" value="提交" />
</form>
</body>
</html>
在瀏覽器中打開后,在表單中輸入:
按提交后,瀏覽器中顯示結(jié)果如下:
在記事本可可看到如下結(jié)果:
從以上結(jié)果可見:
1.PHP函數(shù)nl2br()是在字符串中的每個新行(\r\n)之前插入HTML換行符:<br/>;
2.Windows下的換行是(\r\n);
3.在記事本中,\r或\n均有換行的功能;
相關(guān)文章
提高PHP編程效率的53個要點(經(jīng)驗小結(jié))
下面是php老手整理的一些開發(fā)經(jīng)驗之談,提高php的執(zhí)行效率。2010-09-09php自動識別文字編碼并轉(zhuǎn)換為目標(biāo)編碼的方法
這篇文章主要介紹了php自動識別文字編碼并轉(zhuǎn)換為目標(biāo)編碼的方法,涉及php針對當(dāng)前編碼的判斷與對應(yīng)的編碼轉(zhuǎn)換實現(xiàn)技巧,需要的朋友可以參考下2015-08-08php curl發(fā)起get與post網(wǎng)絡(luò)請求案例詳解
這篇文章主要介紹了php curl發(fā)起get與post網(wǎng)絡(luò)請求案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09PHP CodeBase:將時間顯示為"剛剛""n分鐘/小時前"的方法詳解
本篇文章是對PHP CodeBase:將時間顯示為"剛剛""n分鐘/小時前"的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06