Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8145969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:50:12+00:00 2026-06-06T13:50:12+00:00

I use the strip_tags() function but I need to remove some tags (and all

  • 0

I use the strip_tags() function but I need to remove some tags (and all of their contents).

for example :

<div>
  <p class="test">
    Test A
  </p>
  <span>
    Test B
  </span>
  <div>
    Test C
  </div>
</div>

Let’s say, I need to get rid of the P and SPAN tags, and only keep :

<div>
  <div>
    Test C
  </div>
</div>

strip_tags expects as a second parameter the tags that you want to KEEP.

In this particular example I could use striptags($html, "<div>");
but the html I’m scraping and the tags that need to be removed are different all the time.

I searched for hours for a function that suits my needs, but couldn’t find anything useful.

Any idea’s?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T13:50:15+00:00Added an answer on June 6, 2026 at 1:50 pm

    Use a regular expression. Something like this should work:

    $tags = array( 'p', 'span');
    $text = preg_replace( '#<(' . implode( '|', $tags) . ')>.*?<\/$1>#s', '', $text);
    

    The demo shows it replacing the desired tags with nothing.

    Note that you may need to tweak it more, say, to compensate for whitespace within the tags, or other unknowns that your example does not demonstrate.

    Here is the regex to use to capture tags with or without attributes:

    '#<(' . implode( '|', $tags) . ')(?:[^>]+)?>.*?<\/$1>#s'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some problems using strip_tags PHP function when the string contains 'less than'
In wordpress there is a function of some sort that will strip html tags
trying to use this function http://php.net/manual/en/function.strip-tags.php against this: [link-code-html] => <a href=http://www.kqzyfj.com/click-3782261-10563989>invisibleSHIELD for the
I'd like to use any php function or whatever so that i can remove
i use a php class to make tag cloud from article, but i want
How can i strip <h1>including this content</h1> I know you can use strip tags
I know I can use the ActionView helper strip_tags method in my views to
I'm trying to use the following code and it still strips out all the
use this website a lot but first time posting. My program creates a number
Use case: I've just entered insert mode, and typed some text. Now I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.