我以這樣一個字符串開頭:」我想:\ r \ n \ r \ n1。)擺脫這個混亂\ r \ n \ r \ n \ 2。)現在請「(this是'清理'用戶輸入文本)。PHP - 從此字符串中刪除「 r n r n」的問題:「我想: r n r n1。)解決這個問題 r n r n 2。)現在請「
所以基本上我的發言會是這樣:
$query = sanitize($_POST['query']); // gives the result string
我想從這個字符串中刪除了 「\ r \ n \ r \ n」 的。到目前爲止,我已經嘗試通過這樣做如下:
$query = preg_replace("/\r\n\r\n/", " ", $query);
或
$query = str_replace("\r\n\r\n", " ", $query);
沒有人可以工作嗎?
但是,如果我做到以下幾點:
$query = "I would like to:\r\n\r\n1.) Rid this mess\r\n\r\n\2.) Now Please";
$query = preg_replace("/\r\n\r\n/", " ", $query); // I tried str_replace() too
var_dump($query);
exit;
我得到我想要的輸出...
可能有人請爲什麼地球上,這是發生在我解釋如何我可以解決問題?
任何意見,輸入或建議會因爲我不是從拉我的頭髮幾乎光頭不勝感激......
謝謝!
編輯:
這可能會幫助功能的sanitize():
function html($text)
{
return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
}
function htmlout($text)
{
return html($text);
}
function cleanInput($input)
{
$search = array(
'@<script[^>]*?>.*?</script>@si', // Strip out javascript
'@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags
'@<style[^>]*?>.*?</style>@siU', // Strip style tags properly
'@<![\s\S]*?--[ \t\n\r]*>@' // Strip multi-line comments
);
$output = preg_replace($search, '', $input);
$output = htmlout($output);
return $output;
}
function sanitize($input)
{
if (is_array($input))
{
foreach($input as $var=>$val)
{
$output[$var] = sanitize($val);
}
}
else
{
include "C:/wamp/www/includes/inc/main/db.inc.php";
if (get_magic_quotes_gpc())
{
$input = stripslashes($input);
}
$input = cleanInput($input);
$output = mysqli_real_escape_string($link, $input);
}
return $output;
}
我們是在談論換行或'\ r \ N'文字字符? – 2013-02-11 10:30:37
@ÁlvaroG。Vicario這是從某些用戶輸入文本中刪除換行符的結果。我認爲在這個結果中的翻譯結果是字面字符,因爲這是存儲在我的數據庫中的。我將在第二個帖子中發佈sanitize()代碼... – 2013-02-11 10:34:08
[在PHP中單引號和雙引號字符串之間的區別]可能的重複(http://stackoverflow.com/questions/3446216/difference-between-single-quote - 和 - 雙引號字符串在PHP中) – 2013-02-11 10:36:56