skip to Main Content

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 class="inhoud"><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 class="inhoud">.*?</div>','')

That didn’t do much. What am I doing wrong and what is the correct approach to this?

2

Answers


  1. Chosen as BEST ANSWER

    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 class="inhoud">(?s)(.*?)</div>','')
    

  2. 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 class="inhoud"><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([^>]*)class="(.*?)inhoud(.*?)">(.*?)</div>~im', '', $str);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search