2

How can I with regex in PHP make this

http://www.domain.com/path/to/image/ipsum_image.jpg

Match this and ignore the size part

http://www.domain.com/path/to/image/ipsum_image-300x200.jpg

Keep in mind that 300x200 can be anything, from 4charsx4chars to 1charx1char

2
  • what are you expecting to get? make it clearer, what have you tried? Commented Jul 13, 2012 at 23:07
  • can the image type be png or gif too? Commented Jul 13, 2012 at 23:10

2 Answers 2

3
$subject = 'http://www.domain.com/path/to/image/ipsum_image-300x200.jpg';
$result = preg_replace('&(-[0-9]{1,4}x[0-9]{1,4})&is', '', $subject); 
echo $result;
Sign up to request clarification or add additional context in comments.

1 Comment

s is a meaningless pattern modifier because there are no dots in your pattern representing "any character".
0
$str= "http://www.domain.com/path/to/image/ipsum_image-300x200.jpg";
$regex= '/(\d{1,4})x(\d{1,4}).jpg$/i';
preg_match($regex, $str, $match, PREG_OFFSET_CAPTURE, 3);
print_r($match);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.