Sam Skirrow Sam Skirrow - 7 months ago 13
PHP Question

PHP replace the very last part of a url

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?

Answer

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!

Comments