PHP JSON字符串格式错误 [英] PHP JSON String Malformed

查看:46
本文介绍了PHP JSON字符串格式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个清除用户输入的功能.返回干净的输入后,它将通过json_decode($ var,true);目前,我收到了格式错误的字符串错误.但是,如果我将其打印出来并进行 http://jsonlint.com/进行测试,它将通过.我已经意识到清理过程之后的字符串长149个字符,而字符串之前是85个字符.要解决此问题,我还通过正则表达式运行它以删除特殊字符,但是我认为这可能会撤消之前的内容.功能做到了. "new"函数会撤消filer_var的作用吗?这是清理输入的最佳方法吗?下面是我的代码:

I have a function which cleans users input. After the clean input is returned, it goes through json_decode($var, true); Currently, I'm getting an error of malformed string. Though, if I print it out and test with it http://jsonlint.com/, it passes. I've come to realize that the string after the cleansing processes is 149chars long, and before, its 85. To fix this, I also ran it through a regex to remove special characters, but I'm thinking that may undo what the previous function did. Does the "new" function undo what filer_var does? Is this the best way to clean 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"}

推荐答案

filter_var($ value,FILTER_SANITIZE_STRING,FILTER_FLAG_STRIP_HIGH)的函数调用将创建如下输出: {"name":"Pete Johnson","address":"123 mainstreet","email":"myemail@gmail.com","password":"PA$$word"}

The function call to filter_var($value, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH) creates an output like this: {"name":"Pete Johnson","address":"123 mainstreet","email":"myemail@gmail.com","password":"PA$$word"}

这就是为什么json_decode不起作用的原因.

That is why json_decode does not work.

就像我在评论中说的那样.最好的选择是首先在输入上使用json_decode,然后使用HTML_Purifier和或Zend_Validator遍历各个元素,或者编写自己的元素来处理各个字段.例如,电子邮件具有与密码不同的验证要求.

Like I said in the comments. Your best bet is to use json_decode on the input initially and then run through the individual elements with HTML_Purifier and or Zend_Validator or write your own to deal with individual fields. For example, email has different validation requirements than password.

我尝试运行新功能,但无法正常工作.因此,我进行了一些调整以使其正常运行.尽管我不确定这是否就是您要用于正则表达式的内容.这是我从此代码中得到的输出:

I tried running through the new function, but I couldn't get it to work is. So I made a few adjustments to get it to work. Although I'm not sure if that was what you intended for your regex. Here is what I got as output from the this code:

$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

Output: 34name3434PeteJohnson3434address3434123mainstreet3434email3434myemailgmailcom3434password3434PAword34

这篇关于PHP JSON字符串格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆