-2

I have the following url - this url is not always the same though, but will always end the same:

$thumbnail_url = 'http://i2.ytimg.com/vi/552yWya5RgY/hqdefault.jpg'

using php I'd like to replace hqdefault.jpg with maxresdefault.jpg

so the new thumbnail would look something like this:

$hq_thumbnail_url = 'http://i2.ytimg.com/vi/552yWya5RgY/maxresdefault.jpg'

Is this possible?

Sam Skirrow
  • 3,647
  • 15
  • 54
  • 101

2 Answers2

2

str_replace() is probably your most simple approach...

$thumbnail_url = 'http://i2.ytimg.com/vi/552yWya5RgY/hqdefault.jpg';

$hq_thumbnail_url = str_replace('hqdefault.jpg', 'maxresdefault.jpg', $thumbnail_url);

Hope this helps!

Chris
  • 71
  • 7
0

Here's another way to do it, and it will work even if hqdefault.jpg isn't at the end of the url :

$url = 'http://i2.ytimg.com/vi/552yWya5RgY/hqdefault.jpg';  // Url you want to change
$newImage = 'newimage.jpg';                                 // New filename 

$splitUrl = explode('/', $url); // Split the url at each '/' occurence
$splitUrl[5] = $newImage;       // Change the old filename (hqdefault.jpg) with the new one

$newUrl = implode('/',$splitUrl);  // Reform the url, but this time, with the new filename.
echo $newUrl;                      // Here's the modified url
SamyQc
  • 365
  • 2
  • 10