0

I'm trying to remove <p> and </p> from my JSON rest API output. I did the below but the output it gives me has double slashes like \\r\\n\\r\\n. So how do I change the double slashes to single?

Here's my code

//Remove <p> HTML element and replace with line breaks
$return = str_replace('<p>', '', $return);
$return = str_replace('</p>', '\r\n\r\n', $return);
        
//Output the data in JSON format without escaping the URL slashes
wp_send_json($return, 200, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);

Or can the above me more efficient if I use preg_replace?

2 Answers 2

1

WordPress does this automatically for security reasons. If you get the result on the user side ( frontend ), you can do this using JavaScript and the following code :

const result = response.data.replace(/\/\//g, "/");
1
  • They want to remove double backslashes!
    – Toto
    Mar 4, 2022 at 9:57
0

all is okay you Just need to use json_deocde in front side or where you want to print the result

2
  • Do you mean the receiving side will be able to decode it without an issue?
    – codanad376
    Mar 3, 2022 at 7:48
  • yes offcourse from front_side string can be decoded Mar 3, 2022 at 11:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.