Jump to content

Need Help - Strip any tags with a specific class, but not the content.


AzyureDHikari
Go to solution Solved by AzyureDHikari,

Recommended Posts

Hi everyone, I need help with a coding.

 

This is the HTML

<span class="remove">no tag</span>
<span class="do_not_touch_me">have tag</span>

<h2 class="remove">no tag</h2>
<h3 class="remove" id="this">no tag</h3>
<h4 id="that" class="remove">no tag</h4>

<h2 class="do_not_touch_me2">have tag</h2>
<h3 class="do_not_touch_me3" id="this">have tag</h3>
<h4 id="that" class="do_not_touch_me4">have tag</h4>
I want to strip any tag that have ' class="remove" ' parameter in them. Does not matter if there's other parameter.

As long ' class="remove" ' is there, the tag gets stripped, but not removing the content.

 

This is what it's supposed to return:

no tag
<span class="do_not_touch_me">have tag</span>

no tag
no tag
no tag

<h2 class="do_not_touch_me2">have tag</h2>
<h3 class="do_not_touch_me3" id="this">have tag</h3>
<h4 id="that" class="do_not_touch_me4">have tag</h4>

I've already managed to come out with the function below. All I need is to adjust the preg_replace parameter, but luck is not on my side. Already tried to google for it, but can't seem to find anything that does the job.

 

Any help is much appreciated.

function remove_unwanted_tags_while_saving($content)
{
$tag_to_remove = array("span","div","h2","h3"); 
foreach ($tag_to_remove as $tag)
{
$content= preg_replace("/<\/?" . $tag . "(.|\s)*?>/",'',$content);
}
return $content;
}
Link to comment
Share on other sites

  • Solution

SOLVE. Code to share if anyone needed.

$content = '<span class="remove">no tag</span><br><span class="do_not_touch_me">have tag</span><br><br><h2 class="remove">no tag</h2><br><h3 class="remove" id="this">no tag</h3><br><h4 id="that" class="remove">no tag</h4><br><br><br><h2 class="do_not_touch_me2">have tag</h2><br><h3 class="do_not_touch_me3" id="this">have tag</h3><br><h4 id="that" class="do_not_touch_me4">have tag</h4>';

function remove_unwanted_tags_while_saving($text)
{
	$tags = array('span','h2','h3','h4');
	foreach ($tags as $tag)
	{
		if(preg_match_all('/<'.$tag.'[^>]+class="remove"[^>]*>(.*)<\/'.$tag.'>/iU', $text, $found))
		{
			$text = str_replace($found[0],$found[1],$text);
		}
	}
return $text;
}

echo remove_unwanted_tags_while_saving($content);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.