Search code examples
phpreplacepreg-replacestrip

Remove all text within specific tags


I am interesting in removing all the text within the following tags:

<p class="wp-caption-text">Remove this text</p>

Can anybody give me an idea of how this can be done in php?

Thank you very much


Solution

  • Get rid of the tag and content inside of it:

     $content = preg_replace('/<p\sclass=\"wp\-caption\-text\">[^<]+<\/p>/i', '', $content);
    

    or if you want to preserve the tags:

     $content = preg_replace('/(<p\sclass=\"wp\-caption\-text\">)[^<]+(<\/p>)/i', '$1$2', $content);