我有一个输入文本区域,我想将用户在数据库的文本区域中的换行符存储起来,以便它以与输入文本相同的方式回显输出文本
例如:
Some text some text some text some text
new line some text some text new line
new line some text new line some text
这是我当前的上传脚本:
$sql = "insert into people (price, contact, category, username, fname, lname, expire, filename) values (:price, :contact, :category, :user_id, :fname, :lname, now() + INTERVAL 1 MONTH, :imagename)";
$q = $conn->prepare($sql) or die("failed!");
$q->bindParam(':price', $price, PDO::PARAM_STR);
$q->bindParam(':contact', $contact, PDO::PARAM_STR);
$q->bindParam(':category', $category, PDO::PARAM_STR);
$q->bindParam(':user_id', $user_id, PDO::PARAM_STR);
$q->bindParam(':fname', $fname, PDO::PARAM_STR);
$q->bindParam(':lname', $lname, PDO::PARAM_STR);
$q->bindParam(':imagename', $imagename, PDO::PARAM_STR);
$q->execute();
文本区域中的换行符是换行符,例如 n
。这些不会以 HTML 形式呈现,因此如果您只是回显输出,它们将不会显示。您可以在<textarea>
或<pre>
标记内回显,也可以使用该nl2br()
将新行替换为<br>
标记,以便将其显示为 HTML。
您可以使用 mysql_real_escape_string
在存储之前转义换行符
此处的文档:http://php.net/manual/en/function.mysql-real-escape-string.php
使用 JavaScript 作为n to <br>
通过使用替换来中断文本
str.replace("n", "<br />","g");
我遇到了同样的问题,现在正在工作:
$query = "UPDATE your_table
SET your_text_field = 'line 1'" . 'n' . "'line2'
WHERE your_id_field = " . $id . "';";