7

I have URLs in my string like below:

subdomain.domain.com/ups/a/b.gif
www.domain.com/ups/c/k.gif
subdomain1.domain.com/ups/l/k.docx

Looking to replace all URL like below:

anydomain.com/ups/a/b.gif
anydomain.com/ups/c/k.gif
anydomain.com/ups/l/k.docx

In above string (URL + ups) is common to match. All URLs are started with either HTTP or HTTPS.

3
  • 1
    Have you looked into parse_url()? Commented Mar 16, 2017 at 17:08
  • remove tag name out of the title Commented Mar 20, 2017 at 18:43
  • If any of the answers were helpful you should upvote them, and mark as accepted the one answer that best answered your question. See also stackoverflow.com/help/someone-answers Commented Mar 24, 2017 at 5:11

4 Answers 4

6

As suggested in comments, the way to parse URLs is with parse_url().

<?php
$urls = [
    "http://subdomain.domain.com/ups/a/b.gif",
    "https://www.example.com/ups/c/k.gif",
    "https://subdomain1.domain.com/ups/l/k.docx",
];
$domain = "anydomain.com";
foreach ($urls as &$url) {
    $u = parse_url($url);
    $url = "$u[scheme]://$domain$u[path]" . (isset($u["query"]) ? "?$u[query]" : "");
}
print_r($urls);
Sign up to request clarification or add additional context in comments.

1 Comment

this is the best answer.. never use regex when there is a native way to do it.
5

Maybe it's too late... For a single string:

$components = parse_url( $url);
return str_replace($components['host'], 'anydomain.com', $url);

Protocol in url required. If urls are array - run above in a loop

Comments

2

use:

$new_string = preg_replace("/(http|https):\/\/(?:.*?)\/ups\//i", "$1://anydomain.com/ups/", $old_string);

so for input string:

http://subdomain.domain.com/ups/a/b.gif
https://www.domainX.com/ups/c/k.gif
http://subdomain1.domain.com/ups/l/k.docx

the output will be:

http://anydomain.com/ups/a/b.gif
https://anydomain.com/ups/c/k.gif
http://anydomain.com/ups/l/k.docx

3 Comments

Thanks @Hossam but it should also catch "/ups/" also. It should not work over "subdomain.domain.com/upss" while replace
@DeepanshuGarg : updated the code to catch only url's with "/ups/" after the domain name
You're welcome. You can mark the answer as right & upvote ^__^
0

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
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.