如何在MySQL表中存储格式化的文本? [英] How to store formatted text in MySQL table?

查看:605
本文介绍了如何在MySQL表中存储格式化的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[我的EDITED新问题]

我以HTML格式输入文本为< textarea> 。假设用户输入以下文本:

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

1. Hello     World
2. Hi World

3. Hola

我的PHP代码插入到表中: 1。 Hello World \\\
2。 Hi World\r\\\
\r\\\
3。 Hola

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

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

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

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

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

如何输入用户输入的确切输出?现在只对我的重要性是空格,制表符和新行字符。如下面的答案所述,不建议 nl2br()。任何其他方式?

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?

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

[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.

如果上述不可行,可以保存以下格式,然后也满足我的要求:

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


  1. 空格和制表符

  2. 标点符号和换行符

是否有可以存储此类数据的数据类型? VARCHAR,TEXT和CHAR数据类型呢?请帮助!

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

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

For example: If I type the following text:

Hi!

Hello there!

那么它不应该像

Hi! Hello there!


推荐答案

原始文本仅包含字符,斜体或下划线标签,标点符号和换行符是字符,所以一个简单的varchar会做,如果你真正需要的是这个。

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.

但是,如果你想要的话,你必须决定一个格式化协议粗体,斜体和下划线的文本:HTML,wiki语法,RTF等。如果这种格式是文本的,varchar将会这样做。如果它是二进制的,那么你需要一个blob。

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格式输出页面,其中将空格字符(制表符,空格,换行符等)的序列转换为简单空间。在这里使用< pre>您的HTML转义文本< / pre> 部分,它将正确显示换行符,制表符和多个空格。

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.

这篇关于如何在MySQL表中存储格式化的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆