用 PHP 替换
我有一个帖子表单,它可以将我的文本插入到 MySQL 数据库中.
I have a post form, which inserts my text in a MySQL database.
我用
$post_text = mysql_real_escape_string(htmlspecialchars($_POST['text']));
并且想要替换自动添加的
.
and want to replace the
which was automatically added.
我试过了
$text = str_replace('\r\n','', $text);
$text = str_replace('
','', $text);
$text = str_replace('\R\N','', $text);
$text = str_replace('RN','', $text);
$text = str_replace('/\n','', $text);
$text = str_replace('/r/n','', $text);
$text = str_replace('/R\N','', $text);
$text = str_replace('/R/N','', $text);
但是
总是包含在我的数据库条目中.
but
is always included in my database entries.
我该如何解决这个问题?
How can I fix this?
推荐答案
您尝试过的所有变体的主要问题是
和 是转义字符,只有在双引号字符串中使用时才会转义.
The main problem you have with all the variations you've tried is that both
and are escape characters that are only escaped when you use them in a double-quoted string.
在 PHP 中,'
'
和 "
"
有很大的区别.注意第一个中的单引号,第二个中的双引号.
In PHP, there is a big difference between '
'
and "
"
. Note the single-quotes in the first, and double-quotes in the second.
所以:'
'
将产生一个包含斜杠、一个 'r'、另一个斜杠和一个 'n' 的四字符字符串,而 "
"
将包含两个字符,即换行符和回车符.
So: '
'
will result in a four character string containing a slash, an 'r', another slash and an 'n', whereas "
"
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("
",'', $text);
值得指出的是,这将从输入中删除所有新行,并将它们替换为空.如果输入中有多个新行,它们将全部被删除.在不了解有关您的应用程序的更多信息的情况下,我不知道这是否是您想要的,但这里有一些想法:
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,那么您可能不需要删除它们,因为浏览器不会从
个字符呈现换行符.
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
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.
用户可能提交只有
而没有匹配 的输入,反之亦然(这可能是由于他们的操作系统或浏览器,或故意黑客攻击,或许多其他原因).如果您想替换这两个字符的所有 实例,一种更可靠的方法是单独替换它们,而不是依赖它们彼此相邻.
str_replace()
允许您通过在数组中指定它们来执行此操作.您还可以使用 strtr()
或 preg_replace()
来实现相同的目标.
It is possible that users may submit the input with only
without the matching , 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.
希望有所帮助.
相关文章