用PHP替换\ r \ n [英] Replacing \r\n with PHP

查看:70
本文介绍了用PHP替换\ r \ n的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个邮寄表格,可将我的文本插入到MySQL数据库中.

I have a post form, which inserts my text in a MySQL database.

我使用

$post_text = mysql_real_escape_string(htmlspecialchars($_POST['text']));

,并希望替换自动添加的\r\n.

and want to replace the \r\n which was automatically added.

我尝试了

$text = str_replace('\\r\\n','', $text);
$text = str_replace('\r\n','', $text);
$text = str_replace('\\R\\N','', $text);
$text = str_replace('\R\N','', $text);
$text = str_replace('/\r\\n','', $text);
$text = str_replace('/r/n','', $text);
$text = str_replace('/\R\\N','', $text);
$text = str_replace('/R/N','', $text);

但是\r\n总是包含在我的数据库条目中.

but \r\n is always included in my database entries.

我该如何解决?

推荐答案

您尝试过的所有变体形式的主要问题是,\n\r都是转义字符,只对进行了转义当您在双引号字符串中使用它们时.

The main problem you have with all the variations you've tried is that both \n and \r are escape characters that are only escaped when you use them in a double-quoted string.

在PHP中,'\r\n'"\r\n"之间有很大的区别.注意第一个中的单引号,第二个中的双引号.

In PHP, there is a big difference between '\r\n' and "\r\n". Note the single-quotes in the first, and double-quotes in the second.

因此:'\r\n'将导致包含斜杠,'r',另一个斜杠和'n'的四个字符串,而"\r\n"将包含两个字符,这些字符是换行符和回车符

So: '\r\n' will result in a four character string containing a slash, an 'r', another slash and an 'n', whereas "\r\n" will contain two characters, those being the new line and carriage return characters.

因此,对问题的直接答案是,您需要使用问题中给出的第二个示例,但要用双引号而不是单引号:

So the direct answer to your question is that you need to use the second of the examples you gave in the question, but with double quotes instead of single quotes:

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

值得指出的是,这将从输入中删除 all 行,并将其替换为空.如果输入中有多个新行,则所有新行都将被删除.在不了解您的应用程序的情况下,我不知道这是否是您想要的,但是这里有一些想法:

It's worth pointing out that this will remove all new lines from the input, and replace them with nothing. If there is more than one new line in the input, they will all be removed. Without knowing more about your application, I don't know if this is what you want, but here are some thoughts:

  • 如果只想从输入字符串的 end 中删除空白行(和其他空格),则可以使用trim()函数.

  • If you only want to remove blank lines (and other white space) from the end of the input string, you can use the trim() function instead.

如果要保留输出到HTML的格式,则nl2br()函数将为您提供帮助.如果要输出为不带格式的HTML,则可能不需要删除它们,因为浏览器不会渲染\ n个字符的换行符.

If you want to retain the formatting for output to HTML, then the nl2br() function will help you. If you want to output to HTML without the formatting, then you may not need to remove them, as the browser will not render line breaks from \n characters.

按照示例,如果不使用任何内容替换新行,则第一行的最后一个单词现在将直接进入第二行的第一个单词,依此类推.您可能更喜欢用空格字符而不是空字符代替它们.

If you replace new lines with nothing, as per your example, the last word of the first line will now run directly into the first word of the second line, and so on. You may prefer to replace them with a space character rather than nothing.

用户可能只用\n提交输入而没有匹配的\r,反之亦然(这可能是由于其操作系统或浏览器,故意的黑客攻击或大量输入所致)其他原因).如果要替换这两个字符的 all 实例,一种更可靠的方法是单独替换它们,而不是依靠它们彼此相邻. str_replace()允许您通过在数组中指定它们来执行此操作.您也可以使用strtr()preg_replace()达到相同的目标.

It is possible that users may submit the input with only \n without the matching \r, or vice-versa (this may be due to their OS or browser, or a deliberate hack, or a number of other reasons). If you want to replace all instances of both these characters, a more reliable way to do it would be to replace them individually, rather than relying on them being next to one-another. str_replace() allows you to do this by specifying them in an array. You can also use strtr() or preg_replace() to achieve the same goal.

希望有帮助.

这篇关于用PHP替换\ r \ n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆