PHP str_replace或preg_match或strpos当特定字符串加上if整数时

PHP str_replace或preg_match或strpos当特定字符串加上if整数时,php,preg-match,str-replace,strpos,Php,Preg Match,Str Replace,Strpos,如果存在特定匹配项,我尝试从URL字符串中删除两个字符串和两个整数,例如从WP生成的特色图像裁剪URL: http://website.com/uploads/image1-150x150.jpg http://website.com/uploads/image2-300x160.jpg 基本上,我想留下: http://website.com/uploads/image1.jpg http://website.com/uploads/image2.jpg 因此,我需要在.jpg结尾之前检查

如果存在特定匹配项,我尝试从URL字符串中删除两个字符串和两个整数,例如从WP生成的特色图像裁剪URL:

http://website.com/uploads/image1-150x150.jpg
http://website.com/uploads/image2-300x160.jpg
基本上,我想留下:

http://website.com/uploads/image1.jpg
http://website.com/uploads/image2.jpg
因此,我需要在.jpg结尾之前检查URL的顺序是否为负“-”,然后是INT,然后是字母“x”,然后是INT。如果是这样,那么它应该删除所有这些

最好的方法是什么?我需要怎样做?preg\u replace或str\u replace或strpos?

尝试以下方法:

preg_replace("/-[0-9]*x[0-9]*/", "", $link)
preg_replace('/\\-\d+x\d+\\./', '.', $string);
preg\u replace()
是适用于此的函数

$str = preg_replace('/-\d+x\d+\.jpg$/', '.jpg', $str);
替换正则表达式:

/-[0-9]+x[0-9]+(\.[a-z][a-z][a-z])$/$1
你甚至可以从你的帖子上写下来

/-[0-9]+x[0-9]+/
试试这个:

preg_replace("/-[0-9]*x[0-9]*/", "", $link)
preg_replace('/\\-\d+x\d+\\./', '.', $string);

似乎非常适合
preg_replace()
,为什么不想要这个答案呢?如果文件是png或gif呢?问题在.jpg结尾之前说。我真的接受了。