4

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

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

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

I try:

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

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

3
  • 4
    str_replace("\\n", ... works for me Commented Nov 28, 2017 at 12:58
  • It seems that action_values is a json itself. Commented Nov 28, 2017 at 13:01
  • str_replace("\\n", ... did the trick for me. Thanks B001. If you mark this as an answer Ill mark it as accepted. Commented Nov 28, 2017 at 14:33

2 Answers 2

9

There is no need to remove the \n / new-lines.

Instead you should decode your string using json_decode() and then you can decode the range_from value, which is also json encoded inside your original json:

<?php
$str = '{"status":true,"action_values":"{\n \"range_from\": \"0\",\n \"range_to\": \"0\"\n}"}';

$dec = json_decode($str, true);

var_dump(json_decode($dec['action_values'], true));

Result:

array(2) {
  ["range_from"]=>
  string(1) "0"
  ["range_to"]=>
  string(1) "0"
}

An example.

Sign up to request clarification or add additional context in comments.

Comments

4

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 "\\n" for this task:

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

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

Try the following code and you will see the difference

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

Which will result in the following output:

 -----
 -----\n-----"

The reason for this is that every instance of the "\" character in code, starts a control character. Examples for this are "\n" for newline, "\r" 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 \n, 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 "\n" and '\n'. There are two ways in PHP to create a string:

 $str1 = "hello \n world\n";
 $str2 = 'hello \n world\n';
 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 \n world\n

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

 $trimmed = str_replace('\n', "", $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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.