PHP Regex to Remove http:// from string

PhpRegex

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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

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

Solution 2 - Php

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);

Solution 3 - Php

Better use this:

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

echo $url;

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

Solution 4 - Php

Why not use parse_url instead?

Solution 5 - Php

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

   $str = preg_replace('#^https?\:\/\/([\w*\.]*)#', '', $str);
   echo $str;

Solution 6 - Php

If you insist on using RegEx:

preg_match( "/^(https?:\/\/)?(.+)$/", $input, $matches );
$url = $matches[0][2];

Solution 7 - Php

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;
}

Solution 8 - Php

<?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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCaseyView Question on Stackoverflow
Solution 1 - PhpSarfrazView Answer on Stackoverflow
Solution 2 - PhpShiplu MokaddimView Answer on Stackoverflow
Solution 3 - PhpIfti MahmudView Answer on Stackoverflow
Solution 4 - PhpAmberView Answer on Stackoverflow
Solution 5 - PhpmanufoselaView Answer on Stackoverflow
Solution 6 - PhpOvervView Answer on Stackoverflow
Solution 7 - PhpBrian LewisView Answer on Stackoverflow
Solution 8 - PhpErik SaunierView Answer on Stackoverflow