1
votes

I managed to get the cover picture but i can't get the full size one. I've read on some other stackoverflow post that instead of getting it from facebook api they were replacing the url (what i'm doing now).

https://graph.facebook.com/me?fields=cover

A) At best i would like facebook to return the full size cover if it is possible, so i don't need to do the case B.

B) If case A is not possible, is it possible to change what i'm doing now to regex? I'm just not sure it is the best way to do if case A is not possible.

$total_chars = strlen($response->cover->source);
$last_occurence = strrpos($response->cover->source, '/');
$new_haystack = substr_replace($response->cover->source, '', $last_occurence, ($total_chars - $last_occurence));
$first_occurence = strrpos($new_haystack, '/');
$new_cover_url = substr_replace($response->cover->source, '', ($first_occurence + 1), ($last_occurence - $first_occurence));

since :

https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xpa1/t31.0-8/p180x540/11084131_1381639242163565_3886632224714594501_o.jpg

need to be :

https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xpa1/t31.0-8/11084131_1381639242163565_3886632224714594501_o.jpg

(p180x540/ has been removed).

2
I'm not sure what you're currently doing there. Is the dir you want to remove always p180x540/? If so a str_replace should do the trick.chris85
Except that p180x540 might not always be the same, this is why i didn't use str_replace.Brieuc
What is consistent about that, always pnumbersxnumbers/ or always p3numbersx3numbers/? or maybe not starting with a p always as well?chris85
Hard to tell, i can't know how the url will evolve in the futur, not even sure it would use p or x. Their doc is really poor about it. This is why i guessed i could use the last occurence of "/" since the last part of the url has more chances to stay that way.Brieuc
So it is always the second to last directory you want removed?chris85

2 Answers

2
votes

Here's a non-regex approach. Obviously open to flaws of directory changes but should work in described instance.

$string = 'https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xpa1/t31.0-8/p180x540/11084131_1381639242163565_3886632224714594501_o.jpg';
$replace = array_reverse(explode('/', $string));
echo str_replace($replace[1] .'/', '', $string);
1
votes

Preg_replace will do the job. However the expression i'll join is not solid in terms on unknown corner cases.

    $fb = "https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xpa1/t31.0-8/p180x540/11084131_1381639242163565_3886632224714594501_o.jpg";
    $fb1 = "https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xpa1/t31.0-8/11084131_1381639242163565_3886632224714594501_o.jpg";

    $fb = preg_replace('/p[0-9]+x[0-9]+\//i', '', $fb);

    echo ($fb);