2017-01-13 04:00



I am using a converter that accepts Regex to do a mass "search and replace" in a database to change all .png to .jpg. Consider the following where a link to an image is mixed in with a lot of HTML code (simplified example):


The "bunch_of_text" and "some-x" are all variables.

For clarification, the only .png's I need changed must follow a mostly prescribed URL. For example, I need to replace all .png's on the domain but not on other domains. Here's another example:

Convert: to


XX and YY are variables and may be at any level of depth in the folder structure.

But ignore any other domain other than

The code on the backend is written in PHP.

I need one line of Regex code to return only the ".png" text so my script can replace that with ".jpg". Any help is greatly appreciated.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • duanmei4362 duanmei4362 4年前
    $url = "";
    $result = preg_replace(';(*?/.*?/.*?)\.png;', '$1.jpg', $url);
    echo $result;

    Some regular expression explanation:

    Note I've replaced the usual / delimiter with ; because it's easier when working with URLs.

    点赞 评论 复制链接分享
  • duanniesui6391 duanniesui6391 4年前

    Here is the answer:

    Thanks everyone!

    点赞 评论 复制链接分享
  • dragon0118 dragon0118 4年前

    If literally all you want to return is ".png", then all you need is \.png.

    That will match an instance of ".png". You would then do a Find And Replace All operation:

    Find All:   \.png
    Replace:    \.jpg

    If your question is more complicated than that (i.e. only return ".png" if its context is such-and-such), then please clarify your question.

    点赞 评论 复制链接分享