处理数据库报告中的换行符
新手问题在这里。
我有一个带有文本区域的网络表单,用户自然也会输入换行符。我将此表单存储到数据库中的表中。我还必须据此创建一份报告。所以我将表转储为文本并尝试解析它。对我来说,多个记录之间的分隔符是换行符。但文本区域中的新行字符使我的脚本失效。
我确信以前一定有人遇到过这种情况。您能给我建议如何处理这个问题吗? 我尝试将文本区域中的文本放在双引号中,但这会使我的解析逻辑复杂化。我想知道是否有更简单的方法来处理这个问题。
Newbie question here.
I have a web form with a text area and naturally users will enter newline characters also. I am storing this form to a table in the DB. I also have to create a report from this. So I dump the table to text and am trying to parse it. The delimiters for me between multiple records is a new line character. But the new line character in the text area is throwing my script off.
I am sure someone must have run into this before. Can you suggest me ideas as to how to deal with this?
I tried having the text from the text area in double quotes but that will complicate my parsing logic. I was wondering if there is an easier way to deal with this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为简单的答案是某种形式的逃避。
您可以在 SQL 中执行一些查找/替换,例如 REPLACE(thetext, '\n', '\\n'),或者如果换行符不重要,您可以执行 REPLACE(thetext, '\n', ' ')
I think the simple answer is some form of escaping.
You could do some find/replace in the SQL, such as REPLACE(thetext, '\n', '\\n'), or if the newlines are not important, you could do REPLACE(thetext, '\n', ' ')
最近遇到了同样的问题。最好的方法是在数据转储期间(或在数据到达数据库之前)用其他内容替换换行符。我选择了类似
//
或
的内容,以便稍后恢复新行。Recently ran into the same issue. Your best best is to replace the newline characters during the data dump (or before the data gets to your database) with something else. I opted for something like
//
or<br>
so that I can recover the new lines later.我正在尝试建立一个论坛。问题是当某些用户输入 HTML 时。当您显示他的帖子时,它会被翻译。当显示他的帖子时,我无法移动到下一行。这可能对你有帮助。顺便说一句,我正在使用 PHP。
I was trying to build one forum. The problem was when some user enters HTML. It would get translated when you display his post. And I was not able to move to next line when displaying his post. This might help you. I'm using PHP by the way.