0

Im totally new to regex. I need your help.

What is the regex function for changing the below url to url listed downside? How to use that regex function in PHP.

https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/393656_257350694313804_126044397444435_712409_344887174_s.jpg

TO

https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/s320x320/393656_257350694313804_126044397444435_712409_344887174_n.jpg

OR

https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/s480x480/393656_257350694313804_126044397444435_712409_344887174_n.jpg

Thanks in advance.

3
  • 1
    i think u can use a couple of str_replace here instead of regexp Commented Jul 1, 2012 at 7:36
  • Is the original url layout always going to be [domain]/[folder]/[image]? Commented Jul 1, 2012 at 7:46
  • yes structure is always same. Commented Jul 1, 2012 at 7:48

3 Answers 3

1
$size = "s320x320";
$url = preg_replace("#https://(.*)/(.*)/(.*)\_s.jpg#i", "https://$1/$2/$size/$3_n.jpg", $url);
$url = str_replace("-photos-", "-sphotos-", $url);

This code is untested, but should work. The third line, str_replace, is used to make the regex simpler. :)

Sign up to request clarification or add additional context in comments.

Comments

0
preg_replace( '#^(.*?)photos(.*)(/[^/]*?)_s.jpg$#', '$1sphotos$2/s320x320$3_n.jpg', $url );

2 Comments

domain and filename are also differ
Thanks. Also fbcdn-photos-a.akamaihd.net should converted to fbcdn-sphotos-a.akamaihd.net and _s.jpg should changed to _n.jpg.
0

Ok, let me try to answer with some code

$url = 'https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/393656_257350694313804_126044397444435_712409_344887174_s.jpg';
$str = array('fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4', '_s');
$rep1 = array('fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/s320x320', '_n');
$rep2 = array('fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/s480x480', '_n');

//TO
$url1 = str_replace($str, $rep1, $url);
//OR
$url2 = str_replace($str, $rep2, $url);

1 Comment

Thanks. is it possible change hardcoded string (fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4) to dynamic?

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.