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

时间:2014-09-11 15:36:37

标签: 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

因此,我需要检查,如果URL的顺序为减号" - ",则为INT,然后是字母" x",然后再为INT,之前.jpg结束。如果是这样,那么它应该删除所有这些。

最好的方法是什么,我需要怎样做? preg_replace或str_replace或strpos?

5 个答案:

答案 0 :(得分:0)

试试这个:

preg_replace("/-[0-9]*x[0-9]*/", "", $link)

答案 1 :(得分:0)

preg_replace()是适当的功能。

$str = preg_replace('/-\d+x\d+\.jpg$/', '.jpg', $str);

答案 2 :(得分:0)

替换正则表达式:

/-[0-9]+x[0-9]+(\.[a-z][a-z][a-z])$/$1

你甚至可以从帖子中直接写出来。

答案 3 :(得分:0)

/-[0-9]+x[0-9]+/

Live Preview

//Input: http://website.com/uploads/image1-150x150.jpg
preg_replace("/-[0-9]+x[0-9]+/", "", $input_lines);
//Output: http://website.com/uploads/image1.jpg

答案 4 :(得分:0)

试试这个:

preg_replace('/\\-\d+x\d+\\./', '.', $string);