你會希望使用正則表達式。
這是怎麼回事正則表達式的解釋:
# /^(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);
結果
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
)
你看過[parse_url()](http://php.net/manual/en/function.parse-url.php)嗎? –
從標題中刪除標籤名稱 – Shawn
如果有任何答案對您有幫助,您應該加註他們,並將其標記爲已接受最能解答您問題的答案。另請參閱http://stackoverflow.com/help/someone-answers – miken32