Search code examples
phpstrip

Strip <p> inside of <p> wrapper


Im not sure what to look for to try to achieve this but I was trying to do a str_replace to all the <p> tags and replace them with <br/> and it worked but then I releaized I need to wrap the content with <p>

this is the code i used

<?php
      $nodecontent = render($content);
      $newcontent = preg_replace("/<p[^>]*?>/", "", $nodecontent);
      $newcontent = str_replace("</p>", "<br /><br />", $newcontent);
  print render($newcontent);
?>

this is what my my content had before

  <div property="content:encoded" class="field-item even">
<p>SOME TExT HERE and here and Here..<p>SOME TExT HERE</p> and here and <p>Here..SOME TExT HERE and here and Here..</p>
</p>
</div>

then after I put the code in it looks like this so it did work to some degree but stripped the <p> tag

    <div property="content:encoded" class="field-item even">
SOME TExT HERE and here and Here..<br/><br/>SOME TExT HERE<br/><br/> and here and <br/><br/>Here..SOME TExT HERE and here and Here..<br/><br/>
</div>

but i want it to look like this without stripping the <p> tag as a wrapper

   <div property="content:encoded" class="field-item even"><p>
        SOME TExT HERE and here and Here..<br/><br/>SOME TExT HERE<br/><br/> and here and <br/><br/>Here..SOME TExT HERE and here and Here..<br/><br/>
        </p></div>

Solution

  • between jquery and php i figured this out, not thanks to the others who criticized me and down voted me.

        <script type="text/javascript" >
        jQuery(document).ready(function() {
            jQuery(".field-item").wrapInner('<p></p>')
        });
        </script>
    
        <?php $nodecontent = render($content);
          $newcontent = preg_replace("/<p[^>]*?>/", "", $nodecontent);
          $newcontent = str_replace("</p>", "<br /><br />", $newcontent);
    
     print render($newcontent); ?>