61

Example user input

http://example.com/
http://example.com/topic/
http://example.com/topic/cars/
http://www.example.com/topic/questions/

I want a PHP function to make the output like

example.com
example.com/topic/
example.com/topic/cars/
www.example.com/topic/questions/
0

9 Answers 9

157

ereg_replace is now deprecated, so it is better to use:

$url = preg_replace("(^https?://)", "", $url );

This removes either http:// or https://

Sign up to request clarification or add additional context in comments.

3 Comments

This could have been simply an edit of the original answer (ref).
I'd suggest to remove all possible protocols with "^[a-zA-Z0-9]+://" instead
This is bad practice and only works as long as the protocol never changes.
40

You should use an array of "disallowed" terms and use strpos and str_replace to dynamically remove them from the passed-in URL:

function remove_http($url) {
   $disallowed = array('http://', 'https://');
   foreach($disallowed as $d) {
      if(strpos($url, $d) === 0) {
         return str_replace($d, '', $url);
      }
   }
   return $url;
}

5 Comments

@blur This will return gracefully if the string does not contain any of the "disallowed" strings.
What if the URL is something like: http://domain.com/topic/https://more/? That's a valid url with a valid path, but this approach would mangle it in a way that (I think) the OP wouldn't have intended.
@Lee Good point, I updated my answer to take care of that case.
I really don't think this is the best approach. How do you account for other schemes?
@Madbreaks The OP asked about how they can remove only those two schemes. We can make this more extensible, but it's not necessary to answer the question.
33

I'd suggest using the tools PHP gave you, have a look at parse_url.

<?php
$url = 'http://username:password@hostname/path?arg=value#anchor';

print_r(parse_url($url));

echo parse_url($url, PHP_URL_PATH);
?>

The above example will output:

Array
(
    [scheme] => http
    [host] => hostname
    [user] => username
    [pass] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor
)
/path

It sounds like you're after at least host + path (add others as needed, e.g. query):

$parsed = parse_url('http://www.domain.com/topic/questions/');

echo $parsed['host'], $parsed['path'];

    > www.domain.com/topic/questions/

Cheers

3 Comments

what about fragment and query? the poster did not explicitly say to remove those.
@KimStacks I assume op can figure that out from what I posted, but in the spirit of being pedantic I've updated my answer slightly.
$parsed['host'].$parsed['path'] gives //www.domain.com/topic/questions/
11

Create an array:

$remove = array("http://","https://");

and replace with empty string:

str_replace($remove,"",$url);

it would look something like this:

function removeProtocol($url){
    $remove = array("http://","https://");
    return str_replace($remove,"",$url);
}

Str_replace will return a string if your haystack (input) is a string and you replace your needle(s) in the array with a string. It's nice so you can avoid all the extra looping.

Comments

6

Wow. I came here from google expecting to find a one liner to copy and paste!

You don't need a function to do this because one already exists. Just do:

echo explode("//", "https://anyurl.any.tld/any/directory/structure", 2)[1];

In this example, explode() will return an array of:

["https:", "anyurl.any.tld/any/directory/structure"]

And we want the 2nd element. This will handle http, https, ftp, or pretty much any URI, without needing regex.

https://www.php.net/manual/en/function.explode.php

If you want a function:

function removeProtocols($uri) { return explode("//", $uri, 2)[1]; }

EDIT: See user comment from Harry Lewis... this is my favourite way to do this now.

5 Comments

It absolutely won't return that array, the deliminator won't remain, you'll get "https:" for the first item. This answer also assumes that there are no double slashes anywhere else in the string and also assumes that the url has a protocol in the first place when that could be unknown.
@Shardj edited (we don't use the first array element though!). It makes no assumptions about protocol. If you used input "//anyurl.any.tld/any/directory/structure" it will output that string minus the double slashes. This answer does assume the input is equivalent to all of the examples given in the OP. It won't process urls with additional double slashes (I actually didn't know that was a valid URI!).
To solve the possibility of additional double slashes you can use explode()'s 3rd argument, $limit like so: explode('//', $url, 2);
Love it. Simple and effective for what I need to do.
Of course, if there aren't any double-slashes at all, explode() will return an array containing a single item (the input string), so youd probably just want to always take the last element, i.e.: last(explode('//', $url, 2));
5

You can remove both https and http in one line using a regular expression with preg_replace:

fn (string $url): string
    => preg_replace('~^https?://~', '', $url);

1 Comment

@wlarcheveque: Please see the reference Q&A on this: stackoverflow.com/q/6270004/367456 (also a recent edit should make the comment superfluous, we could delete both comments then).
2

This solution works for me, short one.

parse_url($url, PHP_URL_HOST);

1 Comment

Aside from being the same as my answer, this drops the path, etc.
1

You could use the parse url Functionality of PHP. This will work for all Protocols, even ftp:// or https://

Eiter get the Protocol Component and substr it from the Url, or just concatenate the other Parts back together ...

http://php.net/manual/de/function.parse-url.php

Comments

0
<?php
// user input
$url = 'http://www.example.com/category/website/wordpress/wordpress-security/';
$url0 = 'http://www.example.com/';
$url1 = 'http://www.example.com/category/';
$url2 = 'http://www.example.com/category/website/';
$url3 = 'http://www.example.com/category/website/wordpress/';

// print_r(parse_url($url));
// echo parse_url($url, PHP_URL_PATH);

$removeprotocols = array('http://', 'https://');

echo '<br>' . str_replace($removeprotocols,"",$url0);
echo '<br>' . str_replace($removeprotocols,"",$url1);
echo '<br>' . str_replace($removeprotocols,"",$url2);
echo '<br>' . str_replace($removeprotocols,"",$url3);

?>

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.