Mysql – Replace text on MySQL – Wildcards to replace certain variable parts

MySQLreplacewildcardWordpress

I'm trying to apply a text replace on my WordPress MySQL database. I need to delete part of the current images html code, but this part of the text is variable, althouth there is a part that never changes.

Is there some wildcard that I can use on a MySQL replace?

Example: I would like to replace this:

<a target="_self" href="actualidad/tecnologia/4455-windows-7-surpasses.html"><img src=”http://www.example.com/files/HIDDEN_264_23662_FOTO_win01.jpg" width="215" height="156" /></a>

to

<img src=”http://www.example.com/files/HIDDEN_264_23662_FOTO_win01.jpg" width="215" height="156" />

And delete the href part, so I was thinking in a replace similar to this one:

update wp_posts set post_content = REPLACE 
(post_content, '<a target="_self" href=*<img','<img');

Is there a "*" (or regexp) that takes whatever there is between the 'href' and the '

Best Answer

I usually cheat and dump the database to text, and then use sed (or Notepad++ if the database is small enough). Barring that, it's easier to do it programmatically using Perl or Python.

Doing this kind of manipulation with straight SQL is painful. If you DO do it, I'd recommend strongly that you avoid REPLACE. This isn't what it's normally used for. If you're going to do a bulk change on existing rows, use UPDATE by itself instead.

Related Topic