1cd9c8984f2fdeb996130d54d62a98d9

Ive made a function simply to remove the http string from a given url string.
Kindly see my code below. and is there other way to make it better or make it short?
Thanks

<?php
/*
E.g:
$s = 'http://google.com';
echo remove_http($s);
output: google.com
*/

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

Refactorings

No refactoring yet !

23132e1aa8457e11b243a43b578d51dc

Simo Niemelä

November 11, 2008, November 11, 2008 10:16, permalink

2 ratings. Login to rate!
<?php

function remove_http($url)
{
  return ereg_replace("(https?)://", "", $url);
}

?>
5a00a3a98dcf6f9cd717440fd2b606e5

Eineki

November 11, 2008, November 11, 2008 10:49, permalink

3 ratings. Login to rate!

Hi, you can use preg_replace function to do the job in a single line.

Reference for further investigations:
http://www.php.net/manual/it/function.preg-replace.php

<?
/*
E.g:
$s = 'http://google.com';
echo remove_http($s);
output: google.com
*/

   function remove_http($url='') 
   {
       return preg_replace("/^https?:\/\/(.+)$/i","\\1", $url);
   }

?>
1bf59120892e34eb60836725566c6e55

fain182.myopenid.com

November 13, 2008, November 13, 2008 11:06, permalink

2 ratings. Login to rate!

this should be faster

<?php
/*
E.g:
$s = 'http://google.com';
echo remove_http($s);
output: google.com
*/

function remove_http($url = '')
{
return(str_replace(array('http://','https://'), '', $url));
}
151e36cc7f789a4790c8ca437e3a1f60

Chris Dean

November 15, 2008, November 15, 2008 23:09, permalink

2 ratings. Login to rate!

I'd go with fain182's solution (above) as it is faster (as it doesn't have to load the regex engine) but would strtolower the url first or use str_ireplace() if these urls are coming from user input.

710a543a34a181d528dab05d4550dfed

Manoel b h Carvalho

November 17, 2008, November 17, 2008 18:42, permalink

No rating. Login to rate!

Should be faster, but if passes "http://example.com/?url=http://foo.com" url, the result could be wrong like "example.com/?url=foo.com". I'd use Eineki solution.

1bf59120892e34eb60836725566c6e55

fain182.myopenid.com

November 17, 2008, November 17, 2008 19:42, permalink

1 rating. Login to rate!

this would solve the problem without using regex, and using case-insensitive function

<?php
/*
E.g:
$s = 'http://google.com';
echo remove_http($s);
output: google.com
*/

function remove_http($url = '')
{
    array('http://', 'https://');
    foreach ($list as $word)
        if (strncasecmp($url, $word, strlen($word)) == 0)
            return substr($url, strlen($word));
    return $url;
}
A2c83e34935b70f9b8111f41dce6880b

smithveg-backpack

December 18, 2008, December 18, 2008 09:51, permalink

No rating. Login to rate!

Code above pretty easy. And it work for me.
Thank a lot.

<?php
function remove_http($url)
{
  return ereg_replace("(https?)://", "", $url);
}

?>
21bf1ed3b55610cd702764d0dc83e710

Felipe

April 29, 2009, April 29, 2009 15:30, permalink

No rating. Login to rate!

Another thing:

by:
<a href="http://www.informaticaautonomos.com">Servicios Informáticos - Informatica Autonomos</a>

<?php


function eliminarQueryStringDeUrl($url) {
	$url = split("/", $url);
	return $url[0] ."//". $url[2];
	
}



?>
0897102fa51b9a172cc518aae9356006

vipul

January 12, 2010, January 12, 2010 12:13, permalink

No rating. Login to rate!

hi

For above code if user enter http:// or https:// then its work fine

ie if i enter http://www.test.com then its remove http:// but i also want to remove http://www. or http (whatever enter)

any idea please help me

Thanks in advance

6f314ae3e35965bba56385078ffc83f8

Mark

January 27, 2010, January 27, 2010 19:24, permalink

No rating. Login to rate!

Whew! Thank you! I was trying to figure this out on my own, but thank you for posting this! Helps me alot!

B7eafa9db687dd13b42b83d3cf056774

Software Web App Development

June 3, 2010, June 03, 2010 21:02, permalink

No rating. Login to rate!

Got lot of coding options but i am not sure which to use for the best output. Still i have tried some which works fine.

Dim someURL, someQueryString, splitURL
someURL = "http://localhost/blabla.asp?f=foo&b=boo"
splitURL = split(someURL, "?")

someURL = splitURL(0)
If splitURL(1) <> "" Then

    someQueryString = splitURL(1)

End if
B3a1ed746bef2d134b651ec068d7e99b

Web Design Company

June 7, 2010, June 07, 2010 10:58, permalink

No rating. Login to rate!

@ Vipul : You'll have to work out on solving cannonical issue for your website. If you're running your website with PHP then you need to modify your .htaccess file and if your website has been developed in .NET then you need to configure setting in web.config file.

Let me know if you can't find out the solution.

Cdff5f04128b7cc6c88ef7dbf9e7ca97

Paris City Breaks

July 31, 2010, July 31, 2010 13:17, permalink

No rating. Login to rate!

Nice one, thanks very much for this - I've been trying to get something like this working for ages now, but it looks like fain182s version of this looks the best (mainly because it doesn't need to load the regex engine). Thanks fain182.

2616246dca005acc1f98e43afeafa56f

hi

November 11, 2010, November 11, 2010 04:15, permalink

No rating. Login to rate!
<?php

$yourmum = true;
$blowjobs = true;

while($yourmum %likes% $blowjobs) {
echo 'Oh yea that feels so good, don't stop';
}

?>
91b232917063aa27e6e039a2a9ad367e

immitestawl

June 12, 2011, June 12, 2011 04:27, permalink

No rating. Login to rate!

must check and check coupon code available

Your refactoring





Format Copy from initial code

or Cancel