【问题标题】:Chaning the image url with a regular expression使用正则表达式更改图像 url
【发布时间】:2014-04-17 01:52:35
【问题描述】:

我必须更改一个看起来像这样的网址

http://my-assets.s3.amazonaws.com/uploads/2011/10/PiaggioBeverly-001-106x106.jpg

变成这种格式

http://my-assets.s3.amazonaws.com/uploads/2011/10/106x106/PiaggioBeverly-001.jpg

我知道我需要创建一个正则表达式模式,将初始 url 分为三组:

  1. http://my-assets.s3.amazonaws.com/uploads/
  2. 2011/10/
  3. PiaggioBeverly-001-106x106.jpg

然后从第三组中剪掉分辨率字符串(106x106),去掉最后的连字符,将分辨率移到第二组旁边。知道如何使用preg_replace 之类的东西来完成它吗?

【问题讨论】:

    标签: regex url url-rewriting


    【解决方案1】:

    搜索这个:(.*\/)(\w+-\d+)-(.*?)\.

    并替换为:\1\3/\2.

    在这里演示:http://regex101.com/r/fX7gC2

    【讨论】:

      【解决方案2】:

      模式如下(输入uploads/2011/10/PiaggioBeverly-001-106x106.jpg

      ^(.*/)(.+?)(\d+x\d+)(\.jpg)$
      

      小组将举行如下:

      $1 = uploads/2011/10/
      $2 = PiaggioBeverly-001-
      $3 = 106x106
      $4 = .jpg
      

      现在根据您的需要重新排列。您可以查看此示例from online

      正如你提到的preg_replace(),所以如果它是在PHP中,你可以使用preg_match()

      【讨论】:

        【解决方案3】:
        <?php
        
        $oldurl = "http://my-assets.s3.amazonaws.com/uploads/2011/10/PiaggioBeverly-001-106x106.jpg";
        
        $newurl = preg_replace('%(.*?)/(\w+)-(\w+)-(\w+)\.(\w+)%sim', '$1/$4/$2-$3.jpg', $oldurl);
        
        
        
        echo $newurl;
        #http://my-assets.s3.amazonaws.com/uploads/2011/10/106x106/PiaggioBeverly-001.jpg
        ?>
        

        DEMO

        解释:

        Options: dot matches newline; case insensitive; ^ and $ match at line breaks
        
        Match the regular expression below and capture its match into backreference number 1 «(.*?)»
           Match any single character «.*?»
              Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
        Match the character “/” literally «/»
        Match the regular expression below and capture its match into backreference number 2 «(\w+)»
           Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
              Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
        Match the character “-” literally «-»
        Match the regular expression below and capture its match into backreference number 3 «(\w+)»
           Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
              Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
        Match the character “-” literally «-»
        Match the regular expression below and capture its match into backreference number 4 «(\w+)»
           Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
              Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
        Match the character “.” literally «\.»
        Match the regular expression below and capture its match into backreference number 5 «(\w+)»
           Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
              Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-05-23
          • 1970-01-01
          • 2017-04-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多