简体   繁体   English

nl2br()无法使用我的textarea

[英]nl2br() not working with my textarea

Can somebody please help me with this problem. 有人可以帮我解决这个问题。

I'm simply trying to display a message from users. 我只是想显示来自用户的消息。 The message comes to my database from a textarea. 消息从textarea到达我的数据库。 My problem is when I am typing message and formatting it with breaking into paragraphs it is not display in that format and display it as a one long paragraph. 我的问题是当我输入消息并将其格式化为分段时,它不以该格式显示并将其显示为一个长段。

like this. 像这样。

To change the overall look of your document, choose new Theme elements on the Page Layout tab. 要更改文档的整体外观,请在“页面布局”选项卡上选择新的主题元素。 To change the looks available in the Quick Style gallery, use the Change Current Quick Style Set command. 要更改“快速样式”库中可用的外观,请使用“更改当前快速样式集”命令。 \\r\\n\\r\\nBoth the Themes gallery and the Quick Styles gallery provide reset commands so that you can always restore the look of your document to the original contained in your current template. \\ r \\ n \\ r \\ n \\ n主题库和快速样式库提供重置命令,以便您始终可以将文档的外观恢复为当前模板中包含的原始文件。

I tried with nl2br() and strip_tags() but still no luck 我尝试使用nl2br()和strip_tags(),但仍然没有运气

The Code from validation message. 验证消息中的代码。

// Check for message
if ( !empty ( $_POST['message'])) {
    if ( mb_strlen ( $_POST['message'], 'UTF-8') <= 20 ) {
        $reg_errors['message'] = 'Your message must be greater than 20 characters';
    } else {
        $message = mysqli_real_escape_string($dbc, $_POST['message']);
    }   
} else {
    $reg_errors['message'] = 'Message: This field is required.';
}

This is the code I use when select message from db 这是我从db中选择消息时使用的代码

$message= $row['message'];
$message= nl2br(strip_tags($message));

when echoing $message it print a paragraph like above. 当回显$ message时,它会打印一个如上所示的段落。

You are doing excessive escaping when adding data into database. 在将数据添加到数据库时,您正在进行过多的转义。
If you are using prepared statements, just remove mysqli_real_escape_string() call from your code. 如果您正在使用预准备语句,只需从代码中删除mysqli_real_escape_string()调用即可。
Otherwise you are doing it twice - som you have to find the place where it happens and remove extra mysqli_real_escape_string() call from your code. 否则你要做两次 - 你必须找到它发生的地方,并从你的代码中删除额外的mysqli_real_escape_string()调用。

From my understanding, by using mysqli_real_escape_string() you are making it escape the backslashes so at the time of transaction for example \\r\\n becomes \\\\r\\\\n , however, if those have already been escaped by Javascript then what you end up during transaction is \\\\\\\\r\\\\\\\\n which translates back into html as 根据我的理解,通过使用mysqli_real_escape_string()你将它转义为反斜杠,所以在交易时例如\\r\\n变为\\\\r\\\\n ,但是,如果那些已经被Javascript转义那么你结束了什么在事务期间向上是\\\\\\\\r\\\\\\\\n ,它转换回html为

  1. a literal backward slash 字面反斜杠
  2. r [R
  3. a literal backward slash 字面反斜杠
  4. n ñ

So you might want to replace them with appropriate characters before adding into your database. 因此,在添加到数据库之前,您可能希望用适当的字符替换它们。 Doing that you can pic from a variety of built-in functions such as preg_replace() , strtr() , str_replace() 这样做你可以从各种内置函数pic,如preg_replace()strtr()str_replace() ...

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

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