开发者

how to use <pre> tag with php mysql_real_escape_string with database

I am new to php mysql. I have a textarea and a button. When the user clicks the button, the value in th开发者_如何学编程e textarea is stored in the mysql database. The problem i am facing is...

If the user enters text like

hey this is my text (line break)

and this is my second line text

...the database stores it like:

hey this is my text and this is my second line text

where (linkbreak) which is actually line break by pressing enter key. I add pre tag before saving it in database it works fine but all of its css formating like bold font size and font family gets lost..

how can i solve this problem..


When you store text in a database, think of it as a string of individual characters with no presentational meta data attached (as in, what you see in the textarea that is submitted is not necessarily contained in the string that is posted).

The ability to see it in any format depends on context. The \n will be there, it just needs to be converted into <br /> to be seen on a web page (or in pre or element with white-space: pre).

Also, any colors or other presentational things will be dependent on the CSS used when outputting the string again.


You could also use nl2br($stringfromwebsite) to change all newlines to <br />'s


If you print out the text in PRE and it is displayed with line breaks, then the line breaks are retained. It’s just that HTML doesn’t regard about them in the normal text flow:

Line breaks are also white space characters. Note that although &#x2028; and &#x2029; are defined in [ISO10646] to unambiguously separate lines and paragraphs, respectively, these do not constitute line breaks in HTML, nor does this specification include them in the more general category of white space characters.

[…]

For all HTML elements except PRE, sequences of white space separate "words" (we use the term "word" here to mean "sequences of non-white space characters"). When formatting text, user agents should identify these words and lay them out according to the conventions of the particular written language (script) and target medium.

That’s the reason why HTML introduced the BR elements that represent visual line breaks. You can use nl2br that inserts BR elements in front of each line break character sequence.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜