<?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) «+»