我有许多图像被上传到具有随机文件名的网站,例如
http://www.mysite.com/uploads/images/apicture23.jpg
http://www.mysite.com/uploads/images/anotherpicture203.jpeg
http://www.mysite.com/uploads/images/another.picture203.png
http://www.mysite.com/uploads/images/athird-picture101.gif
Run Code Online (Sandbox Code Playgroud)
是否有可能在PHP中以某种方式在文件扩展名(.jpg,.jpeg .png或.gif)之前立即插入url的另一个字符串,如-300x200?
$out = preg_replace('/\.[a-z]+$/i','-300x200\0',$in);
Run Code Online (Sandbox Code Playgroud)
这基本上是这样做的,从左到右阅读:
它替换以点(\.)开头的任何内容,后跟+字符串中的一个或多个()字符,字符串a-z的end($),case-insensitive(i),-300x200后面跟着匹配的字符串部分(\0).