PHP 替换 url 的最后一部分
PHP replace the very last part of a url
我有以下 url - 这个 url 虽然并不总是相同的,但总是以相同的结尾:
$thumbnail_url = 'http://i2.ytimg.com/vi/552yWya5RgY/hqdefault.jpg'
使用 php 我想用 maxresdefault.jpg
替换 hqdefault.jpg
因此新缩略图将如下所示:
$hq_thumbnail_url = 'http://i2.ytimg.com/vi/552yWya5RgY/maxresdefault.jpg'
这可能吗?
str_replace() 可能是您最简单的方法...
$thumbnail_url = 'http://i2.ytimg.com/vi/552yWya5RgY/hqdefault.jpg';
$hq_thumbnail_url = str_replace('hqdefault.jpg', 'maxresdefault.jpg', $thumbnail_url);
希望对您有所帮助!
这是另一种方法,即使 hqdefault.jpg
不在 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
我有以下 url - 这个 url 虽然并不总是相同的,但总是以相同的结尾:
$thumbnail_url = 'http://i2.ytimg.com/vi/552yWya5RgY/hqdefault.jpg'
使用 php 我想用 maxresdefault.jpg
hqdefault.jpg
因此新缩略图将如下所示:
$hq_thumbnail_url = 'http://i2.ytimg.com/vi/552yWya5RgY/maxresdefault.jpg'
这可能吗?
str_replace() 可能是您最简单的方法...
$thumbnail_url = 'http://i2.ytimg.com/vi/552yWya5RgY/hqdefault.jpg';
$hq_thumbnail_url = str_replace('hqdefault.jpg', 'maxresdefault.jpg', $thumbnail_url);
希望对您有所帮助!
这是另一种方法,即使 hqdefault.jpg
不在 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