我有一個清理用戶輸入的函數。清潔輸入返回後,它通過json_decode($ var,true);目前,我收到錯誤的字符串錯誤。雖然,如果我打印出來並用http://jsonlint.com/進行測試,它會通過。我已經意識到,清理過程之後的字符串是149個字符長,之前是85個。爲了解決這個問題,我還通過一個正則表達式來刪除特殊字符,但我認爲這可能會取消之前的功能確實。 「new」函數是否取消了filer_var的作用?這是清理輸入的最佳方式嗎?下面是我的代碼:PHP JSON字符串格式錯誤
#index.php
$cleanInput = cleanse->cleanInput($_POST);
#cleanse.php OLD
function cleanInput($input){
foreach($input as $key => $value){
$cleanInput[$key] = filter_var($value, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH));
}
return($cleanInput); //Returns 149char long string, visually 85chars
}
#cleanse.php NEW
function cleanInput($input){
foreach($input as $key => $value){
$cleanInput[$key] = preg_replace("[^+A-Za-z0-9]", "", filter_var($value, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH)));
}
return($cleanInput); //Returns 85char long string, visually 85chars
}
#outputs
#Before
{"name":"Pete Johnson","address":"123 main street","email":"[email protected]","password":"PA$$word"}
#After
{"name":"Pete Johnson","address":"123 main street","email":"[email protected]","password":"PA$$word"}
我們可以看到實際輸入的每一步嗎?清潔之前和之後? – mpen 2012-03-24 17:08:24
上面增加了輸出。 – 2012-03-24 17:14:32
什麼是您的POST數組中的原始帖子值?在附註中您是否嘗試過HTMLPurifier? http://htmlpurifier.org/這通常是我使用,而不是內置的過濾器功能。 – Gohn67 2012-03-24 17:31:30