PHP string JSON Malformed

I have a function that clears user input. After returning clean input, it goes through json_decode ($ var, true); I am currently getting a garbled string error. Although, if I print it and test it with http://jsonlint.com/ , it will pass. I realized that the line after the cleaning processes lasts 149chars and earlier, its 85. To fix this, I also ran it through a regular expression to remove special characters, but I think this can cancel the previous function did. "New" function cancels filer_var action? Is this the best way to clear input? Below is my code:

#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":"myemail@gmail.com","password":"PA$$word"}

  #After
    {"name":"Pete Johnson","address":"123 main street","email":"myemail@gmail.com","password":"PA$$word"}
+2
source share
1 answer

filter_var ($ value, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH) : {"name":"Pete Johnson","address":"123 mainstreet","email":"myemail@gmail.com","password":"PA$$word"}

json_decode .

. json_decode , HTML_Purifier Zend_Validator . , , .

EDIT:

, . , . , , . :

$input = '{"name":"Pete Johnson","address":"123 main street","email":"myemail@gmail.com","password":"PA$$word"}';
$cleanedInput = preg_replace("/[^+A-Za-z0-9]/", "", filter_var($input, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH));
echo $cleanedInput;

:      34name3434PeteJohnson3434address3434123mainstreet3434email3434myemailgmailcom3434password3434PAword34

+3

All Articles