I am trying to remove a specific div and it's content from a large number of Wordpress posts. Instead of just using 'display:none' to make the div invisible, I would like it to be removed permanently. That's why I prefer to remove the div and it's content from the SQL database. The div looks like this:
<div ><h3>abc bac</h3><ul>
<li><a href="a">abc bac</a></li>
<li><a href="b">abc bac</a></li>
<li><a href="c">abc bac</a></li></ul>
</div>
In short, I would like all divs with the class inhoud
to be removed from the posts content.
I have tried several things, including this regular expression:
UPDATE `wp_posts` SET `post_content` = REGEXP_REPLACE(post_content,'<div >.*?</div>','')
That didn't do much. What am I doing wrong and what is the correct approach to this?
CodePudding user response:
Why you just dont do one php foreach with all results from this table and then replace the results of that field with simple regex:
$str = '<div ><h3>abc bac</h3><ul> <li><a href="a">abc bac</a></li> <li><a href="b">abc bac</a></li> <li><a href="c">abc bac</a></li></ul> </div> <div> your next data</div>';
$str = preg_replace('~<div([^>]*)>(.*?)</div>~im', '', $str);
CodePudding user response:
After a couple of hours I finally managed to do it with this SQL update:
UPDATE `wp_posts` SET `post_content` = REGEXP_REPLACE(post_content,'<div >(?s)(.*?)</div>','')