为什么我使用nl2br()会出现\n\r?

发布于 2024-11-18 03:52:08 字数 1360 浏览 0 评论 0原文

有人刚刚在我的网站上创建了一个群组,其中包含 \n\r 的描述。问题是我已经对描述文本执行了此操作:

//Convert all urls to links
$group_description = preg_replace('#([\s|^])(www)#i', '$1http://$2', $group_description);
$pattern = '#((http|https|ftp|telnet|news|gopher|file|wais):\/\/[^\s]+)#i';
$replacement = '<a href="$1" target="_blank">$1</a>';
$group_description = preg_replace($pattern, $replacement, $group_description);

$group_description = str_replace("\'" , "'", $group_description );
$group_description = nl2br($group_description);

/* Convert all E-mail matches to appropriate HTML links */
$pattern = '#([0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.';
$pattern .= '[a-wyz][a-z](fo|g|l|m|mes|o|op|pa|ro|seum|t|u|v|z)?)#i';
$replacement = '<a href="mailto:\\1">\\1</a>';
$group_description = preg_replace($pattern, $replacement, $group_description);

您可以在此处看到这种情况发生

您会看到上面写着\n\r 欢迎加入

为什么它没有成为
标签?

这可能很重要,这是我首先如何将此文本放入数据库的方法:

$group_description = mysql_real_escape_string($_POST['group_description']);
// And then insert the $group_description into the db without doing any more operations on it.

知道为什么我仍然得到 \n\r 吗?

Someone just made a group on my site which has a description with \n\r. The thing is that I already do this to the description text:

//Convert all urls to links
$group_description = preg_replace('#([\s|^])(www)#i', '$1http://$2', $group_description);
$pattern = '#((http|https|ftp|telnet|news|gopher|file|wais):\/\/[^\s]+)#i';
$replacement = '<a href="$1" target="_blank">$1</a>';
$group_description = preg_replace($pattern, $replacement, $group_description);

$group_description = str_replace("\'" , "'", $group_description );
$group_description = nl2br($group_description);

/* Convert all E-mail matches to appropriate HTML links */
$pattern = '#([0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.';
$pattern .= '[a-wyz][a-z](fo|g|l|m|mes|o|op|pa|ro|seum|t|u|v|z)?)#i';
$replacement = '<a href="mailto:\\1">\\1</a>';
$group_description = preg_replace($pattern, $replacement, $group_description);

You can see this happening here.

You see it says there \n\r Welcome to Join.

Why didn't it become a <br /> tag?

This is probably important, here is how I put this text into the db in the first place:

$group_description = mysql_real_escape_string($_POST['group_description']);
// And then insert the $group_description into the db without doing any more operations on it.

Any idea why I am still getting the \n\r?

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

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

发布评论

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

评论(2

掀纱窥君容 2024-11-25 03:52:08

问题是 \r 和 \n 在数据库中分别存储为“\”“r”和“\”“\n”。它们不是存储为回车符或换行符,而是存储为斜杠字符后跟字母。当您调用 nl2br() 时,它会查找换行符,而不是它的文本表示形式。希望这是有道理的。

您可以使用正则表达式 (preg_replace) 或 str_replace 来替换它们。但问题可能是由数据插入数据库的方式引起的。

例如:

$string = str_replace('\n', "\n", $string);
$string = str_replace('\r', "\r", $string);

您会注意到我对第一个使用了单引号,对第二个使用了双引号。使用单引号时,“\r”和“\n”将存储为文本,而不是特殊字符。这可能就是为什么它们没有正确存储在您的数据库中的原因。

The problem is that the \r and \n are stored in your database as "\" "r" and "\" "\n" respectively. They're not stored as a carriage return or new line character but the slash character followed by the letter. When you're calling nl2br(), it's looking for the new line character, not the textual representation of it. Hopefully that makes sense.

You could use a regular expression (preg_replace) or str_replace to replace them. But the problem probably is caused by how the data is inserted in your database.

For instance:

$string = str_replace('\n', "\n", $string);
$string = str_replace('\r', "\r", $string);

You'll notice that I used single quotes for the first one and double for the second. When using single quotes, the "\r" and "\n" are stored as text, not the special characters. That could by why they're not stored properly in your database.

尴尬癌患者 2024-11-25 03:52:08

你自己已经回答了。 mysql_real_escape_string 还将换行符转换为转义序列 \r\n

您必须首先应用转换(包括 nl2br)并在 mysql_query 之前执行数据库转义/编组。 (但你知道,还有更容易使用的数据库 API。)

You've answered it yourself. mysql_real_escape_string also converts linebreaks into the escape sequences \r and \n.

You must apply your conversions first (including nl2br) and do the database escaping/marshalling right before the mysql_query. (But you know, there are also much easier to use database APIs.)

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