問題描述
我有一個帖子表單,它可以將我的文本插入到 MySQL 數(shù)據(jù)庫中.
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);
但是
總是包含在我的數(shù)據(jù)庫條目中.
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 中,'
'
和 "
"
有很大的區(qū)別.注意第一個中的單引號,第二個中的雙引號.
In PHP, there is a big difference between '
'
and "
"
. Note the single-quotes in the first, and double-quotes in the second.
所以:'
'
將產(chǎn)生一個包含斜杠、一個 '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()
函數(shù)相反.
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()
函數(shù)將幫助您.如果您想在沒有格式的情況下輸出到 HTML,那么您可能不需要刪除它們,因為瀏覽器不會從
個字符呈現(xià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
characters.
如果你什么都不替換新行,根據(jù)你的例子,第一行的最后一個詞現(xiàn)在將直接進入第二行的第一個詞,依此類推.您可能更喜歡用空格字符替換它們,而不是什么都不替換.
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.
用戶可能提交只有
而沒有匹配
的輸入,反之亦然(這可能是由于他們的操作系統(tǒng)或瀏覽器,或故意黑客攻擊,或許多其他原因).如果您想替換這兩個字符的所有 實例,一種更可靠的方法是單獨替換它們,而不是依賴它們彼此相鄰.str_replace()
允許您通過在數(shù)組中指定它們來執(zhí)行此操作.您還可以使用 strtr()
或 preg_replace()
來實現(xiàn)相同的目標.
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.
希望有所幫助.
這篇關于用 PHP 替換 的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!