skip to Main Content

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>', 'rnrn', $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


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

    Login or Signup to reply.
  2. 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, "/");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search