我有以下网址结构:
http://website.com/images/folder1/folder2/123456/x500x1000_2x_ImageName.jpg
我需要从图片名称中移除裁剪分辨率“x500x1000_2x_”,如下所示:
http://website.com/uploads/folder1/folder2/123456/ImageName.jpg
我尝试了很多东西:
$img = preg_replace('/\[x](\d+)[x](\d+)[_2x_]\.*/', '', $img);
$img = preg_replace('/[x]\d[x]\d[_2x_]\.*/', '', $img);
$img = preg_replace('/\/\[x]+\d+[x]+\d+\D+\d\.*/', '', $img);
我对preg_replace真的不太好,有人能帮帮我吗?
答案 0 :(得分:3)
这应该适合你:
(这里我只是用basename()
替换网址preg_replace()
的x\d+x\d+_\d+x_
。最后我再次将网址与dirname()
连接在一起)
<?php
$img = "http://website.com/images/folder1/folder2/123456/x500x1000_2x_ImageName.jpg";
echo $img = dirname($img) . "/" . preg_replace("/x\d+x\d+_\d+x_/", "", basename($img));
?>
输出:
http://website.com/images/folder1/folder2/123456/ImageName.jpg
正则表达式解释:
x\d+x\d+_\d+x_