繁体   English   中英

php mysql存储数据库中文本区域的换行符

[英]php mysql storing line breaks in text area in database

我有一个输入文本区域,我想存储用户在数据库的文本区域中的换行符,以便它以与输入文本中相同的方式回显输出文本

例如:

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 . "';";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM