How do I remove http, https and slash from user input in php

PhpFunctionPreg Replace

Php Problem Overview


Example user input

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

I want a php function to make the output like

domain.com
domain.com/topic/
domain.com/topic/cars/
www.domain.com/topic/questions/

Let me know :)

Php Solutions


Solution 1 - Php

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

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

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

Solution 2 - Php

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

Solution 3 - Php

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

Solution 4 - Php

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.

Solution 5 - Php

You can remove both https and http in one line using ereg_replace:

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

Solution 6 - Php

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.

Solution 7 - Php

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

Solution 8 - Php

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

?>

Solution 9 - Php

if its the first characters in the string you can use substr(0,8) , and it will remove the first 8th character if its not use the "str_replace()" function http://php.net/manual/en/function.str-replace.php

Solution 10 - Php

Found this http://refactormycode.com/codes/598-remove-http-from-url-string

function remove_http($url = '')
{
	if ($url == 'http://' OR $url == 'https://')
	{
		return $url;
	}
	$matches = substr($url, 0, 7);
	if ($matches=='http://') 
	{
		$url = substr($url, 7);		
	}
	else
	{
		$matches = substr($url, 0, 8);
		if ($matches=='https://') 
		$url = substr($url, 8);
	}
	return $url;
}

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
QuestionBlurView Question on Stackoverflow
Solution 1 - Phpuser1985367View Answer on Stackoverflow
Solution 2 - PhpJacob RelkinView Answer on Stackoverflow
Solution 3 - PhpMadbreaksView Answer on Stackoverflow
Solution 4 - PhpJordan CaseyView Answer on Stackoverflow
Solution 5 - PhpminazView Answer on Stackoverflow
Solution 6 - Phpmike-sourceView Answer on Stackoverflow
Solution 7 - PhpPaul WeberView Answer on Stackoverflow
Solution 8 - PhpOpenWebWarView Answer on Stackoverflow
Solution 9 - PhpIyad Al aqelView Answer on Stackoverflow
Solution 10 - PhpIves.meView Answer on Stackoverflow