nl2br 不适用于 ical 摘要换行符“\n”

发布于 2024-11-07 22:43:54 字数 553 浏览 2 评论 0原文

我正在收集 PHP 中的一个事件的摘要。问题是,摘要包含换行符 \n,我想在插入事件时将其替换为

在我的 PHPMyAdmin 中,转义 ical 摘要后,我看到字符 \n,但如果不转义 ical 摘要,我就看不到字符 \n。然而,无需逃避,我就可以看到真正的换行符。我需要逃避 ical 摘要以确保我的数据库安全。使用 nl2br 函数在这两种情况下都不起作用?为什么?

代码:

//without escaping
$title = $vevent->getProperty('summary');//Object method which retrieves the summary of an event 
$title = nl2br($title);

//with escaping
$title = mysql_real_escape_string($vevent->getProperty('summary'));
$title = nl2br($title);

I'm collecting a summary of an ical event in PHP. The thing is, that the summary contains line breaks \nand I want to replace them with <br> when inserting the events.

In my PHPMyAdmin after escaping the ical summary I see the characters \n, but without escaping the ical summary I can't see the characters \n. However, without escaping I can see actual real line-breaks. I need to escape the ical summary to make my database safe. Using the nl2br function is not working in both cases? Why?

CODE:

//without escaping
$title = $vevent->getProperty('summary');//Object method which retrieves the summary of an event 
$title = nl2br($title);

//with escaping
$title = mysql_real_escape_string($vevent->getProperty('summary'));
$title = nl2br($title);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

゛时过境迁 2024-11-14 22:43:54

嗯,朋友,阅读手册页面怎么样? http://php.net/manual/en/function.mysql -real-escape-string.php

mysql_real_escape_string() 调用 MySQL 的库函数 mysql_real_escape_string,该函数在以下字符前面添加反斜杠:\x00、\n、

您的换行符已经被 SQL 转义。所以你之前需要nl2br。

我向大家发出的通常的咆哮是:为什么你在 2011 年使用 mysql 扩展?它在五年前就已经过时了。使用 mysqli 或 PDO 和准备好的语句,那么你不需要担心转义。

Hrm, what about reading the handbook page, my friend? http://php.net/manual/en/function.mysql-real-escape-string.php says

mysql_real_escape_string() calls MySQL's library function mysql_real_escape_string, which prepends backslashes to the following characters: \x00, \n,

Your line breaks are already SQL escaped. so you need to nl2br before.

And the usual rant I give to everyone: why are you using the mysql extension in 2011? It went out of fashion half a decade ago. Use mysqli or PDO and prepared statements, then you dont need to worry about escaping.

卸妝后依然美 2024-11-14 22:43:54

您可以以相反的方式执行此操作:

//with escaping
$title = nl2br($title);
$title = mysql_real_escape_string($vevent->getProperty('summary'));

但是您不需要在插入数据库之前调用 nl2br。输出数据到浏览器时最好做nl2br。这样,您可以将实际数据存储在数据库中(稍后可以在其他上下文中使用),并在输出之前使用 HTML 对其进行格式化。

You can do it the other way around:

//with escaping
$title = nl2br($title);
$title = mysql_real_escape_string($vevent->getProperty('summary'));

But you shouldn't need to call nl2br before inserting into the database. It's better to do nl2br when you output the data to the browser. That way you store the actual data in the database (which later can be used in other context) and format it with HTML before outputting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文