halliewuud
halliewuud

Reputation: 2785

php regex ignoring size from image url

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

Upvotes: 2

Views: 296

Answers (2)

Besnik
Besnik

Reputation: 6529

$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;

Upvotes: 3

worenga
worenga

Reputation: 5856

$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);

Upvotes: 0

Related Questions