我有一个 url src:
$url = 'http://www.test.com/uploads/2013/06/test.jpg';
我的代码:
preg_replace('/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '-220x220', $url);
如何使用preg_replace
结果是
$url = 'http://www.test.com/uploads/2013/06/test-220x220.jpg';
我有一个 url src:
$url = 'http://www.test.com/uploads/2013/06/test.jpg';
我的代码:
preg_replace('/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '-220x220', $url);
如何使用preg_replace
结果是
$url = 'http://www.test.com/uploads/2013/06/test-220x220.jpg';
似乎您想让 size 部分成为可选的:
preg_replace('/(-\d+x\d+)?(?=\.(jpg|jpeg|png|gif)$)/i', '-220x220', $url);
如果目标只是'-220x220'
在文件名和扩展名之间插入,则可以使用:
$url = 'http://www.test.com/uploads/2013/06/test.jpg';
$file = pathinfo(basename($url));
if (in_array($file['extension'], array('jpg', 'jpeg', 'png', 'gif')) {
$url = dirname($url) . '/' . $file['filename'] . '-220x220.' .$file['extension'];
}