51

I have full URLs as strings, but I want to remove the http:// at the beginning of the string to display the URL nicely (ex: www.google.com instead of http://www.google.com)

Can someone help?

Casey
  • 881
  • 2
  • 9
  • 14
  • 3
    Why do you need a regex? Why not just remove the first 7 characters? – Oliver Charlesworth Mar 03 '12 at 21:08
  • Check this one: http://stackoverflow.com/questions/4875085/php-remove-http-from-link-title – stefandoorn Mar 03 '12 at 21:09
  • @OliCharlesworth: It can be 8 characters as well with `https://` – Sarfraz Mar 03 '12 at 21:11
  • 1
    If you don't need to use regex, don't. `str_replace` is faster than regex, and easier to read for other people looking at your code. – VettelS Mar 03 '12 at 21:20
  • possible duplicate of [Remove http from variable](http://stackoverflow.com/q/7293799/), [Parsing Domain From URL In PHP](http://stackoverflow.com/q/276516/), [How to remove first part of url in PHP?](http://stackoverflow.com/q/5867310/). – outis Apr 25 '12 at 10:07
  • @VettelS: not appreciably faster. If just 'http:' scheme needs to be handled, then `str_replace` is preferabel, but if both 'http:' and 'https:' schemes need to be covered, then regex approach is likely faster & more readable. '%^https?://%' isn't that difficult to read. – outis Apr 25 '12 at 10:10
  • @outis How do you guys know which function or library is faster? Where do you get those benchmarks or what procedure do you follow to weigh the speed of a code, library or function? I'm fascinated, and curious! – Ifti Mahmud Feb 15 '18 at 12:33

8 Answers8

151
$str = 'http://www.google.com';
$str = preg_replace('#^https?://#', '', $str);
echo $str; // www.google.com

That will work for both http:// and https://

outis
  • 75,655
  • 22
  • 151
  • 221
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
24

You don't need regular expression at all. Use str_replace instead.

str_replace('http://', '', $subject);
str_replace('https://', '', $subject);

Combined into a single operation as follows:

str_replace(array('http://','https://'), '', $urlString);
Fiasco Labs
  • 6,457
  • 3
  • 32
  • 43
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • 4
    This will also strip out any subsequent matches of http(s)://, which may not be a problem - but it could be. For example if it is used in a query string without proper urlencoding – aland Apr 30 '14 at 18:31
20

Better use this:

$url = parse_url($url);  
$url = $url['host'];

echo $url;

Simpler and works for http:// https:// ftp:// and almost all prefixes.

Ifti Mahmud
  • 862
  • 10
  • 15
  • 1
    Except that it will throw away any path and query information as well as the transport protocol. So while it works successfully on the OPs example, it's not actually the correct answer for their question. – piersb Mar 02 '16 at 16:48
  • 2
    @piersb I can't fully agree with you on this. The code successfully delivers the result Casey was looking for. Besides, it was written to serve one purpose. If you want to show path or query information you can certainly do so (http://php.net/manual/en/function.parse-url.php). However, I've found one problem with the code. If we try to parse the url without specifying the protocol, it shows an error and that's something I'm annoyed with right now :/ – Ifti Mahmud May 29 '16 at 18:35
4

Why not use parse_url instead?

Amber
  • 507,862
  • 82
  • 626
  • 550
3

To remove http://domain ( or https ) and to get the path:

   $str = preg_replace('#^https?\:\/\/([\w*\.]*)#', '', $str);
   echo $str;
MartinTeeVarga
  • 10,478
  • 12
  • 61
  • 98
manufosela
  • 469
  • 6
  • 8
1

If you insist on using RegEx:

preg_match( "/^(https?:\/\/)?(.+)$/", $input, $matches );
$url = $matches[0][2];
Overv
  • 8,433
  • 2
  • 40
  • 70
  • 2
    Just for the sake of completeness, I'd add a `s?` after the http. And yeah, I know it wasn't in his question . . . :)) – Mike Ryan Mar 03 '12 at 21:10
0

Yeah, I think that str_replace() and substr() are faster and cleaner than regex. Here is a safe fast function for it. It's easy to see exactly what it does. Note: return substr($url, 7) and substr($url, 8), if you also want to remove the //.

// slash-slash protocol remove https:// or http:// and leave // - if it's not a string starting with https:// or http:// return whatever was passed in
function universal_http_https_protocol($url) {  
  // Breakout - give back bad passed in value
  if (empty($url) || !is_string($url)) {
    return $url;
  }  

  // starts with http://
  if (strlen($url) >= 7 && "http://" === substr($url, 0, 7)) {
    // slash-slash protocol - remove https: leaving //
    return substr($url, 5);
  }
  // starts with https://
  elseif (strlen($url) >= 8 && "https://" === substr($url, 0, 8)) {
    // slash-slash protocol - remove https: leaving //
    return substr($url, 6);
  }

  // no match, return unchanged string
  return $url;
}
Brian Lewis
  • 152
  • 2
  • 4
0
<?php
    // (PHP 4, PHP 5, PHP 7)
    // preg_replace — Perform a regular expression search and replace

$array = [
    'https://lemon-kiwi.co',
    'http://lemon-kiwi.co',
    'lemon-kiwi.co',
    'www.lemon-kiwi.co',
];

foreach( $array as $value ){
    $url = preg_replace("(^https?://)", "", $value );
}

This code output :

lemon-kiwi.co
lemon-kiwi.co
lemon-kiwi.co
www.lemon-kiwi.co

See documentation PHP preg_replace

Erik Saunier
  • 8,670
  • 1
  • 20
  • 15