skip to Main Content

Most answers here are about converting line breaks to <p> but I’m looking to do the opposite. I need <p> to be rnrn.

Because the WCFM marketplace plugin for WordPress is forcing the use of HTML elements but I need to output my description as JSON object as below, without HTML.

{
    "name": "Hijo",
    "description": "<p>Too cute to avoid. You'd so wanna bring her home.</p><p>Each of our pets is .....",
}

Or is there an opposite function to wpautop()?

2

Answers


  1. you can use preg_match function to find and replace the <p> with rn, an </p> with blank space

    Login or Signup to reply.
  2. You can just use strtr to swap the strings.

    $description = "<p>Too cute to avoid. You'd so wanna bring her home.</p><p>Each of our pets is </p><p>3rd Line.....";
    
    $descriptionLf = strtr($description,['<p>' => '','</p>' => "rnrn"]);
    
    echo '<pre>'.$descriptionLf.'</pre>';
    

    Note: The special characters like n must be in double quotes.

    Output:

    Too cute to avoid. You'd so wanna bring her home.
    
    Each of our pets is 
    
    3rd Line.....
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search