Regex - Remove anchor tags wrapped around img tags

I'm currently working on a very large WordPress build that is heavily populated with posts. I've had a request come through where I have to do the following...

Remove all anchor tags that are wrapped around img tags.

For example, I need the following...

<a href="some-random-page"><img src="some-radom-image"/></a>

To become...

<img src="some-random-image"/>

Could this at all be done using an SQL update statement that I can run in phpMyAdmin as all WordPress posts are stored in the "posts" table.

Hope someone can help.

Solutions

You can do so with Dreamweaver. Export your db table . Open in Dreamweaver.

Find the following with "regular expression" option checked.

<a href="([^>]*)"><img src="([^>]*)"/></a>

Now replace this with:

<img src=$2/>

Now import the db table back to MySQL.

For match use : (<a[^<]*>.*?)(<img[^>]*>)([^<]*</a>)

For replace use : $2

Example : http://RegExr.com?37vbk

(replace dummy text in the example with your string)

Similar questions

anchor wrapped li for Slide-out menu in wordpress
i'm trying to do something like this: http://cssdeck.com/labs/3fo47n21 in wordpress, but i'm having trouble to get the navigation to wrap menu li with anchor tags like this: All i got is this in the header: You guys are my last resort, any advice?
Adding link tags and rel="lightbox" around images Regex
How can i change this example when i just got the <image> tag and i want to add link tags around them and the rel lightbox attribute as well? i cant figure it out. I'm just not good with regular expressions at all. the example So in my case i have <img src="...." class="...." alt="....."> and i need <a href="....." rel="lightbox" tit...
removing <p> tags around img, iframes and also scripts
I've been working with embeded content and as many already know, wordpress wraps the_content() lines in p tags. So, found this: When I change "iframe" by "script" it works, but then only for one of them, I need both beacuse the twitter embbeds create a hidden script element wich in turn creates a ghost paragraph.
remove anchor element around Wordpress images with filter (or jquery)
I have an anchor element like this: (It's the standard way of Wordpress to embed uploaded pictures in a post.) I want to remove the anchor around the image element, but keep the image. I simply want the image to show without being clickable. This could be done either with a filter for the content of a post in Wordpress or after the page is loaded w...
What is the best way to convert the img tags into amp-img?
I have a wordpress website and the images that come from the the_content() function have the <img> tag, however for the AMP pages they should be written like this: <amp-img>. What's the correct way of doing that in WP?
I'm using a filter to remove the <p> tags auto wrap, but there still wrapped elements?
I have this in my functions.php: The images inside the slider were added as custom post types called "Page Content." I'm not sure if the filter only applies to the main Posts. The the posts in the admin look like this: But something still wrapping the images of my Nivo Slider with p tags: CSS: Any suggestions to remove the p tags?

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .