在表中使用什么类型的排序规则
我有一个简单的问题,希望有一个简单的解决方案: 我有一个用 PHP 和 HTML 编写的网站,使用带有 MySQL 的 Linux 服务器。 它有一个表单,用户可以在其中填写一些个人信息,包括一个文本区域,其中 他们的目的是复制并粘贴测试简历。
我还为我的客户设置了一个后端,她可以在其中查询数据库以查看谁 注册并检索他们的信息。
我的问题是,当我查询并回显包含以下内容的表行的内容时 CV(大量文本),换行符全部消失了 - 所有内容都打印在一行中。
有人知道我是否可以通过使用正确的排序规则/字符编码来解决这个问题 对于包含用户简历的特定行?我希望存在这样的排序规则来保存和维护换行符。
I have what is a simple problem that hopefully has a simple solution:
I have a site written in PHP and HTML, using a Linux server with MySQL.
It has a form where users fill in some personal info, including a textarea in which
they are meant to copy and paste a test CV.
I have also set up a back end for my client where she can query the database to see who
registered and retrieve their info.
My problem is that when I query and echo the content of the table row that contains the
CV (alot of text), the line breaks are all gone - everything is printed in one line.
Does someone know if I can solve this by using the right kind of collation/character encoding
for that specific row that contains the users's cvs? I am hoping that such collation exists that saves and maintains line breaks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
排序规则与它无关 - 排序规则和字符集根本不会触及您的换行符。如果您想查看它,请查看回显文本的页面源。
然而,在正常情况下,HTML 将换行符视为所有其他空白,因此当您将换行符回显到浏览器时,它们将不可见。无论如何,您不应该将纯文本输出为 HTML,因为它们不一样。您必须先将纯文本转换为 HTML;一个简单的方法是在文本上调用
htmlspecialchars()
和nl2br()
(按这个顺序,否则 htmlspecialchars 会吃掉你新创建的 br 标签并将它们变成 < code>。如果不这样做,不仅会产生不需要的输出,还可能造成重大安全风险 (XSS)。
Collation has nothing to do with it - collations and charsets won't touch your newlines at all. If you want to see it, look at the page source of the echo'd text.
HTML, however, treats line breaks like all other whitespace under normal circumstances, so they won't be visible when you echo them to a browser. You shouldn't be outputting plain text as HTML anyway, because they're not the same. You must convert the plain text to HTML first; a simple method is to call
htmlspecialchars()
andnl2br()
on the text (in that order, otherwise htmlspecialchars will eat your newly-created br tags and turn them into<br/>
. Failing to do so will not only create undesired output, it can also be a major security risk (XSS).使用
nl2br($text)
添加 HTML 换行符。Use
nl2br($text)
to add HTML line breaks.我不认为整理与此相关。
textarea
中的换行采用\n
或\r
字符的形式。如果您没有做任何“奇怪”的事情,那么这些断线应该存储到数据库中。我认为你的问题是当你回显表格的内容时,由于浏览器不会将
\n
和\r
显示为新行,因此你必须替换它们用于
元素或将每个段落包装在中
您可以使用 nl2br() 为此。
I don't think collation is related to this. Break lines from the
textarea
come in the form of the\n
or\r
characters. If you are not doing anything "weird" those break lines should be stored into the DB.I think your problem is when you echo the content of the table, since the browser doesn't display the
\n
and\r
as new lines, you have to either substitute them for<br/>
element or wrap each paragraph in a<p></p>
You can use nl2br() for that.
或者如何将文本包装在
中,
请参阅:http://www.w3schools.com/tags/tag_pre.asp
or how about wrapping the text in a
<pre>
</pre>
see: http://www.w3schools.com/tags/tag_pre.asp