How can PHP be used to manipulate URLs and change specific parts of them, such as adding a subfolder like "thumbnails"?

To manipulate URLs and change specific parts of them in PHP, you can use functions like parse_url() to break down the URL into its components, modify the desired parts, and then reconstruct the URL using functions like http_build_url(). For example, to add a subfolder like "thumbnails" to a URL, you can append it to the path component of the URL.

$url = "http://example.com/images/photo.jpg";
$parts = parse_url($url);

if(isset($parts['path'])){
    $parts['path'] = rtrim($parts['path'], '/') . '/thumbnails/' . basename($parts['path']);
}

$newUrl = http_build_url($parts);
echo $newUrl;