56

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/
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Blur
  • 1,216
  • 3
  • 12
  • 17

9 Answers9

149

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

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

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

J0e3gan
  • 8,740
  • 10
  • 53
  • 80
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;
}
Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • @blur This will return gracefully if the string does not contain any of the "disallowed" strings. – Jacob Relkin Dec 05 '10 at 06:37
  • 3
    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 Dec 05 '10 at 06:51
  • @Lee Good point, I updated my answer to take care of that case. – Jacob Relkin Dec 05 '10 at 06:54
  • I really don't think this is the best approach. How do you account for other schemes? – Madbreaks Jan 16 '13 at 22:33
  • 1
    @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. – Jacob Relkin Jan 17 '13 at 06:44
31

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

Madbreaks
  • 19,094
  • 7
  • 58
  • 72
10

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.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Jordan Casey
  • 955
  • 9
  • 16
5

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.

mike-source
  • 1,004
  • 3
  • 17
  • 32
  • 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 Mar 05 '20 at 12:25
  • @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!). – mike-source Mar 05 '20 at 14:34
  • 2
    To solve the possibility of additional double slashes you can use `explode()`'s 3rd argument, `$limit` like so: `explode('//', $url, 2);` – Harry Lewis May 29 '20 at 10:07
  • 2
    Love it. Simple and effective for what I need to do. – Brett Donald Jul 24 '20 at 23:35
  • 1
    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));` – Harry Lewis Sep 21 '20 at 17:01
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);
hakre
  • 193,403
  • 52
  • 435
  • 836
minaz
  • 5,690
  • 1
  • 32
  • 29
  • @wlarcheveque: Please see the reference Q&A on this: https://stackoverflow.com/q/6270004/367456 (also a recent edit should make the comment superfluous, we could delete both comments then). – hakre Jul 02 '22 at 08:53
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

Paul Weber
  • 6,518
  • 3
  • 43
  • 52
1

This solution works for me, short one.

parse_url($url, PHP_URL_HOST);
WK5
  • 302
  • 2
  • 12
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);

?>
halfer
  • 19,824
  • 17
  • 99
  • 186
OpenWebWar
  • 580
  • 8
  • 16