2

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?

3
  • Edit the filename? Not sure the purpose of what you are trying to do, so I don't know what direction to try and take an answer. Commented Feb 13, 2013 at 5:14
  • @Jon : I only want to remove that resolution string i.e. (600x417) from that image url so that it will take original image resolution instead of taking 600x417. Commented Feb 13, 2013 at 5:16
  • Ooh, ok, doing a preg_replace with the pattern /\-*(\d+)x(\d+)/ for the answer below shall do the trick ^^ Commented Feb 13, 2013 at 5:18

4 Answers 4

5

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);
Sign up to request clarification or add additional context in comments.

2 Comments

@Some1.Kill.The.DJ : Yup it is.
@Some1.Kill.The.DJ : Edited the code, now it wont check for all the pattern.
1

You can try

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

Replace with:$1$2

Comments

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

Comments

0

preg_replace

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

There are a lot of ways.

1 Comment

you don't need to escape . if it's within []

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.