You'll want to make use of regular expressions.
An explanation of what's going on with the regular expression:
# /^(http[s]?:\/\/).*?\/(.*)$/
#
# / starting delimiter
# ^ match start of string
# (http[s]?:\/\) match http:// or https://
# .*? match all characters until the next matched character
# \/ match a / slash
# (.*) match the rest of the string
#
# in the replacement
#
# $1 = https:// or https://
# $2 = path on the url
$urls = [
'https://subdomain.example.org/ups/a/b.gif',
'http://www.example.org/ups/c/k.gif',
'https://subdomain1.example.org/ups/l/k.docx'
];
foreach($urls as $key => $url) {
$urls[$key] = preg_replace('/^(http[s]?:\/\/).*?\/ups\/(.*)$/', '$1anydomain.com/ups/$2', $url);
}
print_r($urls);
Result
Array
(
[0] => https://anydomain.com/ups/a/b.gif
[1] => http://anydomain.com/ups/c/k.gif
[2] => https://anydomain.com/ups/l/k.docx
)