开发者

Problems displaying paragraphs when retrieving text from a database

开发者 https://www.devze.com 2023-03-29 13:46 出处:网络
I am having problems displaying chucks of text, with paragraphs, from a database. For example, if I pass in a string such as:

I am having problems displaying chucks of text, with paragraphs, from a database.

For example, if I pass in a string such as:

local lotsOfText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n\nNunc euismod justo sapien, at sollicitudin lacus. Quisque vestibulum commodo felis id posuere."

Then the \n\n creates a paragraph break in the text, which is what I would expect.

However, if I retrieve exactly the same string as above from a database, for example:

local lotsOfText = row.Info

Then the \n\n are displayed on the screen, rather than paragraph break.

Any 开发者_StackOverflow社区help would be gratefully received!


Thanks everyone for their ideas and suggestions. Based on what BMitch said, I tried a different approach and believe this is actually a problem with my database, rather than Lua or the string itself.

If I create a DB table in code and insert the string, I can them retrieve it correctly. I thin k this is due to the data type I was using, or maybe something to do with how I am editing data in Lita.


I had almost the exact same issue when I was writing a short build script in Python. The problem turned out to be that Python was treating the string result returned from a process call to be binary. I imagine Lua is doing the same thing. Although I've never personally encountered this and I couldn't find a direct solution via Google, someone else mentioned the lpack library for packing & unpacking binary data in this answer. Also, the string.dump and loadstring functions may point you toward a solution, in case lpack doesn't do what you're looking for, but I think the key thing is finding out an existing way (or writing your own) to interpret the "binary" data as characters.


Escapes are only processed when inside a single quoted or double quoted string literal.

The problem is that your database is storing '\n' instead of actually storing a paragraph break.

If you can't fix the database, you can process it afterwards with a smart gsub:

mystring = string.gsub(mystring,[[\(%a)]],{n="\n",r="\r",t="\t"})
0

精彩评论

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