1

Am looking to remove a certain string from my wordpress content. I found that the only way to do it is to run mysql query in database using regex_replace. I just couldn't figure out how to use the regex to replace this content., The string is

<ul class="md_wholebook_list"><li class="dZip"><a href="https://example.com/demo/file.zip" title="file.zip" download>Download ZIP</a></li></ul>

The string https://example.com/demo/file.zip and file.zip will be the varying parameters.

This is what I did,

<ul class="md_wholebook_list"><li class="dZip"><a href="(.*?)\.zip" title="(.*?)\.zip" download>Download ZIP</a></li></ul>

Actually I don't wanna mess with my database, so any wordpress function or php function is much better.If at all only mysql query works, am willing to take the shot.

This is the sql query I ran through my phpmyadmin.

SELECT REGEXP_REPLACE(`post_content`,'<ul class="md_wholebook_list"><li class="dZip"><a href="(.*?)\.zip" title="(.*?)\.zip" download>Download ZIP</a></li></ul>','')

Is tat right?

  • What's the result you're getting with this regex? – Robo Mop Feb 13 '20 at 10:27
  • The `.` before `zip` in `(.*?).zip` needs to be escaped, otherwise it will look for any character preceding `zip`, rather than a literal `.` character – Zaelin Goodman Feb 13 '20 at 12:20
  • With my above command, I don't see any result at all. – shanganithi Feb 13 '20 at 12:35
  • @ZaelinGoodman I tried escaping the . character, but I don't see anything getting removed. – shanganithi Feb 13 '20 at 13:43
  • You need a lookbehind in your regexp: `((?<=(href=\"))|(?<=(title=\")))(.*?)\.zip`. It will check if the string you are looking for is following href=" or title=". Use a regExp tool to adjust your regexp's: https://regex101.com/. Also *always* test your regexp's on strings it *must not* match – Jonathan Pauw Feb 13 '20 at 14:12

0 Answers0