开发者

What type of collation to use in a table

开发者 https://www.devze.com 2023-04-03 10:17 出处:网络
I have what is a simple problem that hopefully has a simple solution: I have a s开发者_StackOverflow中文版ite written in PHP and HTML, using a Linux server with MySQL.

I have what is a simple problem that hopefully has a simple solution: I have a s开发者_StackOverflow中文版ite 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.


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() and nl2br() 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).


Use nl2br($text) to add HTML line breaks.


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.


or how about wrapping the text in a <pre> </pre>

see: http://www.w3schools.com/tags/tag_pre.asp

0

精彩评论

暂无评论...
验证码 换一张
取 消