php 如何在 MySQL 表中存储格式化文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6574646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:45:55  来源:igfitidea点击:

How to store formatted text in MySQL table?

phpmysqldatabase-designtypes

提问by Sumit Gupta

[My EDITED New Question]

[我编辑过的新问题]

I am taking text input in HTML form as <textarea>. Suppose the user entered the following text:

我将 HTML 形式的文本输入作为<textarea>. 假设用户输入了以下文本:

1. Hello     World
2. Hi World

3. Hola

My PHP code is inserting into the table as: 1. Hello World\r\n2. Hi World\r\n\r\n3. Hola

我的 PHP 代码插入到表中: 1. Hello World\r\n2. Hi World\r\n\r\n3. Hola

I am displaying this text into a DIV element by using the below method (assume that $textis retrieved from database):

我使用以下方法将此文本显示到 DIV 元素中(假设$text从数据库中检索):

<div><?php echo $text ?></div>

<div><?php echo $text ?></div>

The output I am getting is: 1. Hello World 2. Hi World 3. Hola

我得到的输出是: 1. Hello World 2. Hi World 3. Hola

How to get the exact output as user entered? Only importance to me right now is spaces, tabs and new line characters. As mentioned in the below answers, nl2br()is not suggested. Any other way?

如何获得用户输入的确切输出?现在对我来说唯一重要的是空格、制表符和换行符。如以下答案所述,nl2br()不建议使用。还有什么办法吗?



[My Old Question]I want to store formatted text into a mysql table. By formatted, I mean to preserve proper bold characters, italics, underline, spaces, tabs, punctuation marks, newline characters etc.

[我的老问题]我想将格式化的文本存储到 mysql 表中。通过格式化,我的意思是保留适当的粗体字符、斜体、下划线、空格、制表符、标点符号、换行符等。

If the above is not possible, if I can preserve the following formatting then also my requirement is fulfilled:

如果以上是不可能的,如果我可以保留以下格式,那么我的要求也得到满足:

  1. spaces and tabs
  2. punctuation marks and newline characters
  1. 空格和制表符
  2. 标点符号和换行符

Is there any data type which can store such data? What about VARCHAR, TEXT and CHAR data types? Please help!

是否有任何数据类型可以存储此类数据?VARCHAR、TEXT 和 CHAR 数据类型呢?请帮忙!

For example: If I type the following text:

例如:如果我输入以下文本:

Hi!

Hello there!

then it should NOT print like

那么它不应该像

Hi! Hello there!

回答by JB Nizet

Raw text include characters only, not formatting like bold, italics or underline. Tabs, punctuation marks and newline are characters, so a simple varchar will do if all you really need is this.

原始文本仅包含字符,不包含粗体、斜体或下划线等格式。制表符、标点符号和换行符都是字符,所以如果你真正需要的只是这个,一个简单的 varchar 就可以了。

But you have to decide on a formatting protocol if you want bold, italics and underlined text: HTML, wiki syntax, RTF, etc. If this format is textual, a varchar will do. If it's binary, you'll need a blob.

但是,如果您想要粗体、斜体和带下划线的文本,则必须确定格式协议:HTML、wiki 语法、RTF 等。如果此格式是文本格式,则使用 varchar 即可。如果是二进制文件,则需要一个 blob。

If you have newlines in your text and it's displayed on a single line, it's probably because you output it in a HTML page, where sequences of space characters (tabs, spaces, newlines, etc.) are converted to a simple space. Use a <pre>your HTML-escaped text here</pre>section, and it will display the newlines, tabs and multiple spaces correctly.

如果您的文本中有换行符并且它显示在一行中,这可能是因为您在 HTML 页面中输出它,其中空格字符序列(制表符、空格、换行符等)被转换为一个简单的空格。使用<pre>your HTML-escaped text here</pre>节,它将正确显示换行符、制表符和多个空格。

回答by Bruno Alano

I think you can store with utf_encode(). and get it with utf_decode(). You can see the n12brfunction of PHP.

我想你可以用utf_encode(). 并得到它utf_decode()。可以看到n12brPHP的功能。

-- EDIT --

- 编辑 -

I encrypt using base64_encode()and to get I use base64_decode(). Tabs, spaces and special characters are preserved.

我加密使用base64_encode()并让我使用base64_decode(). 制表符、空格和特殊字符被保留。

回答by Lightness Races in Orbit

Spaces and punctuation arepreserved.

空格和标点保留。

If you're not seeing them, then you're probably outputtingthe text in an HTML document and forgetting to mark it up as HTML: remember, web browsers ignore newlines and consecutive spaces in plaintext.

如果您没有看到它们,那么您可能将文本输出到 HTML 文档中,而忘记将其标记为 HTML:请记住,Web 浏览器会忽略纯文本中的换行符和连续空格。

As for formatting like bold, you need to come up with some way of marking-upyour text to store the formatting information. You might consider HTML or Markdown.

至于像粗体这样的格式,你需要想出一些方法来标记你的文本来存储格式信息。您可以考虑使用 HTML 或 Markdown。