dsgoj7457 2017-11-28 12:54
浏览 109
已采纳

PHP - 从JSON响应中删除

I have a (simplified) JSON response from an API call that looks like below

{"status":true,"action_values":"{
 \"range_from\": \"0\",
 \"range_to\": \"0\"
}"}

I am trying to remove the characters from above using PHP but it doesn't seem to be working.

I try:

$trimmed = str_replace("
", "", $response);

Where $response is my JSON string as above. But this does not remove/replace the character.

  • 写回答

2条回答 默认 最新

  • dongxing1960 2017-11-28 13:38
    关注

    I would recommend the solution by @jeroen, since it makes use of PHPs native functions to handle JSON.

    However, since you've asked, I sense that you do not completely understand why your solution did not work.

    As already pointed out in the comments by @B001 you need "\ " for this task:

    $trimmed = str_replace("\
    ", "", $response);
    

    The reason for this is, that " " represents the new line character when "\ " represents the string " ".

    Try the following code and you will see the difference

     print("-----");
     print("
    "); 
     print("-----");
     print("\
    ");
     print("-----");
     print("\"");
    

    Which will result in the following output:

     -----
     -----
    -----"
    

    The reason for this is that every instance of the "\" character in code, starts a control character. Examples for this are " " for newline, "" for carriage return, "\t" for tab, \" for the "-character inside a string defined by "" and "\\" for the actual backslash-character.

    So if you want to create the actual string containing , you have to tell the interpreter that you actually want the \-character and not a control character created by \ and what ever character follows. This is done by using double backslashes "\\" which is the string representation of the actual backslash string. This is called "escaping".

    In your case you have the actual character string in your $response variable, and you therefore have to use the escaped character as pattern.

    Last let me explain the difference between " " and ' '. There are two ways in PHP to create a string:

     $str1 = "hello 
     world
    ";
     $str2 = 'hello 
     world
    ';
     print($str1);
     print($str2);
    

    Both variables will contain a string, however, the "-string indicates for the PHP interpreter that the contained string should be interpreted, while the '-string gives you the string as it is. The example above would therefor result in the following output:

     hello
     world
     hello 
     world
    
    

    This shows that the following code also would strip your string of the instances since ' ' would contain the actual string and not the control character:

     $trimmed = str_replace('
    ', "", $response);
    

    This interpretation of the "-string goes so far as to allow for variables to be inserted into a string:

     $name = "Daniel";
     $age = 18;
     $sentence = "My Friend $name is $age years old.";
     print($sentence);
    

    and would result in:

     My Friend Daniel is 18 years old.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器