麻辣家族討論版版's Archiver

小誌 發表於 2011-4-19 09:56

5-6-3 多行文字輸入元件(Textarea)

[color=DarkRed][size=4][b]5-6-3   多行文字輸入元件(Textarea)[/b][/size][/color]
如果輸入的資料量不多,那麼單行文字輸入元件是最適合的元件,但是,在需要大量輸入資料的場合就應該使用多行文字輸入元件(Textarea)比較適合:[code]<textarea name=”識別名” cols=”欄數” rows=”列數”></textarea>[/code]多行文字輸入元件利用「cols」屬性來設定顯示的寬度(字數),因為它可以輸入多行文字,所以又有「rows」屬性來設定顯示的高度(行數),因為多行文字輸入元件可輸入的資料量比較大,當表單中含有多行文字輸入元件時,表單資料的傳遞方式應採用「Post」,以避免傳送的資料被截斷。
為避免輸入的資料中含有HTML標籤而造成困擾,多行文字輸入元件也多會配合「htmlspecialchars()」函數來處理。
textarea.php[code]<html><head>
<title>多行文字輸入元件使用</title>
</head><body>
<form action="textarea_ans.php" method="Post">
請輸入留言:<Br>
<Textarea name="content" cols="35" rows="5">
</Textarea><Br>
<input type="Submit">
</form>
</body></html>[/code]

小誌 發表於 2011-4-19 09:57

另外,當我們將輸入於多行文字輸入元件的資料顯示出來時,會有一個斷行的問題,輸入資料時,在多行文字輸入元件中按下「Enter」會有換行的效果,雖然在表面上看不到任何的換行字元,但實際上卻真實存在著一個換行字元,這個換行字元在我們輸出到瀏覽器時並不會有換行的效果,因此,輸出時應先使用「nl2br()」函數將這個換行字元替換成「<Br>」的HTML標籤符號。
nl2br ()函數格式[code]string nl2br ( string string)[/code][attach]5528[/attach]
圖5-23  多行文字輸入元件。
textarea_ans.php[code]<html><head>
<title>多行文字輸入元件使用</title>
</head><body>
<?
$mycontent=$_REQUEST["content"];
?>
原始資料(未置換):<Br>
<?=$mycontent?>
<P>
原始資料(已置換):<Br>
<?=nl2br($mycontent)?>
</body></html>[/code][attach]5529[/attach]
圖5-24  使用「nl2br()」函數變更換行符號為HTML標籤。

小誌 發表於 2011-4-19 10:00

有時候,我們的輸入版面與資料輸入版面的配置不同,如果依原始資料格式來輸出,可能造成我們網頁排版的問題,因此,亦可以使用「wordwrap()」函數來指定每一行資料的輸出長度:
wordwrap()函數格式[code]string wordwrap ( string str [, int width [, string break [, boolean cut]]])[/code]範例textarea_2_ans.php執行後,如下圖,請配合textarea_2.php表單輸入網頁:
[attach]5530[/attach]
圖5-25  文字折行。
textarea_2_ans.php[code]<html><head>
<title>多行文字輸入元件使用</title>
</head><body>
<?
$mycontent=$_REQUEST["content"];
?>
原始資料(未換行):<Br>
<?=$mycontent?>
<P>
原始資料(已換行):<Br>
<?
$newword=wordwrap($mycontent,40,"<br>",1);
echo "$newword";
?>
</body></html>[/code]

小誌 發表於 2011-4-19 10:00

我們常在留言板或討論區中見到有些網友很喜歡罵髒話,要解決出現不雅字句的辦法,就是將髒話過濾掉,把它換成文字或乾脆消除它,若要置換掉字串中的某些字句可使用「str_replace()」函數:
str_replace()函數格式[code]mixed str_replace ( mixed search, mixed replace, mixed subject [, int &count])[/code]例如:範例ex5_14.php[code]<?
$testword="你真是個壞蛋";
$newword=str_replace("壞蛋","bad man",$testword);
echo $newword; //輸出'你真是個bad man'
?>[/code]

頁: [1]

麻辣家族討論版版為 麻辣學園 網站成員  由 昱得資訊工作室 © Since 1993 所提供