J.K.A.
J.K.A.

Reputation: 7404

Remove resolution string from image url in PHP

I have following image url:

http://www.example.org/wp-content/blogs.dir/29/files/2013/02/Personalized-Results-Asterisk-600x417.png

Here url containing by default resolution i.e. 600x417.png in it. I want to remove this resolution from this image url.

Final output of image url should be like this :

http://www.example.org/wp-content/blogs.dir/29/files/2013/02/Personalized-Results-Asterisk.png

How can I do this?

Upvotes: 2

Views: 1406

Answers (4)

Prasanth Bendra
Prasanth Bendra

Reputation: 32740

Try this :

$string = 'http://www.example.org/wp-content/blogs.dir/29/files/2013/02/Personalized-Results-Asterisk-600x417.png';
$pattern = '/\-*(\d+)x(\d+)\.(.*)$/';
$replacement = '.$3';
echo preg_replace($pattern, $replacement, $string);

Upvotes: 5

Anirudha
Anirudha

Reputation: 32797

You can try

Regex:^(.*?)-\d+x\d+\.([^/]+)$

Replace with:$1$2

Upvotes: 1

Emery King
Emery King

Reputation: 3534

preg_replace

$correct_url = preg_replace('`\-[0-9]*x[0-9]*(\.[^\.]*)$`','$1',$url);

There are a lot of ways.

Upvotes: 0

realization
realization

Reputation: 597

$str=preg_replace("/^(.+)-\d+?x\d+?(\.\w+)$/i","$1$2",$str);

Upvotes: 0

Related Questions