Settings | Sign in | Sign up

The author has 1 post.

【资料】nl2br函数的使用:正确显示文本区输入的内容

Floor 1 巨大八爪鱼 11/21/15 12:56

表单:
<form id="form1" name="form1" method="get">
  <label for="content">Text Area:</label>
  <br>
  <textarea name="content" cols="50" rows="10" id="content"><?=$content?></textarea>
  <br>
  <input type="submit" value="Submit" style="margin-top:6px">
</form>
使用get方式提交

其中默认值$content一定要经过htmlspecialchars转义后才能放到textarea标签内部。


获取文本区输入的内容:
<?php
if (filter_has_var(INPUT_GET, 'content')) {
 $content = htmlspecialchars($_GET['content']); // 转义HTML特殊字符
} else {
 $content = '';
}
?>


显示输入的内容:
<div style="font-weight: bold">You typed:</div>
<div><?=nl2br($content)?></div>


其中nl2br把文本区中的\n转换成<br>,以便于在HTML上换行。

Content converter:

Reply the post
Content:
User: You are currently anonymous.
Captcha:
Unclear? Try another one.