处理数据库报告中的换行符

发布于 2024-09-08 16:58:00 字数 235 浏览 5 评论 0原文

新手问题在这里。

我有一个带有文本区域的网络表单,用户自然也会输入换行符。我将此表单存储到数据库中的表中。我还必须据此创建一份报告。所以我将表转储为文本并尝试解析它。对我来说,多个记录之间的分隔符是换行符。但文本区域中的新行字符使我的脚本失效。

我确信以前一定有人遇到过这种情况。您能给我建议如何处理这个问题吗? 我尝试将文本区域中的文本放在双引号中,但这会使我的解析逻辑复杂化。我想知道是否有更简单的方法来处理这个问题。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤芳又自赏 2024-09-15 16:58:00

我认为简单的答案是某种形式的逃避。

您可以在 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', ' ')

深海少女心 2024-09-15 16:58:00

最近遇到了同样的问题。最好的方法是在数据转储期间(或在数据到达数据库之前)用其他内容替换换行符。我选择了类似 //
的内容,以便稍后恢复新行。

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.

柠檬心 2024-09-15 16:58:00

我正在尝试建立一个论坛。问题是当某些用户输入 HTML 时。当您显示他的帖子时,它会被翻译。当显示他的帖子时,我无法移动到下一行。这可能对你有帮助。顺便说一句,我正在使用 PHP。

$pattern = array('/</','/>/','/\n/');
$replace = array('<','>','<br>');
$post = preg_replace($pattern,$replace,$post);

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.

$pattern = array('/</','/>/','/\n/');
$replace = array('<','>','<br>');
$post = preg_replace($pattern,$replace,$post);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文