<?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 !
Simo Niemelä
November 11, 2008, November 11, 2008 10:16, permalink
<?php
function remove_http($url)
{
return ereg_replace("(https?)://", "", $url);
}
?>
Eineki
November 11, 2008, November 11, 2008 10:49, permalink
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);
}
?>
fain182.myopenid.com
November 13, 2008, November 13, 2008 11:06, permalink
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));
}
Chris Dean
November 15, 2008, November 15, 2008 23:09, permalink
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.
Manoel b h Carvalho
November 17, 2008, November 17, 2008 18:42, permalink
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.
fain182.myopenid.com
November 17, 2008, November 17, 2008 19:42, permalink
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;
}
smithveg-backpack
December 18, 2008, December 18, 2008 09:51, permalink
Code above pretty easy. And it work for me.
Thank a lot.
<?php
function remove_http($url)
{
return ereg_replace("(https?)://", "", $url);
}
?>
Felipe
April 29, 2009, April 29, 2009 15:30, permalink
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];
}
?>
vipul
January 12, 2010, January 12, 2010 12:13, permalink
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
Mark
January 27, 2010, January 27, 2010 19:24, permalink
Whew! Thank you! I was trying to figure this out on my own, but thank you for posting this! Helps me alot!
Software Web App Development
June 3, 2010, June 03, 2010 21:02, permalink
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
Web Design Company
June 7, 2010, June 07, 2010 10:58, permalink
@ 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.
Paris City Breaks
July 31, 2010, July 31, 2010 13:17, permalink
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.
hi
November 11, 2010, November 11, 2010 04:15, permalink
<?php
$yourmum = true;
$blowjobs = true;
while($yourmum %likes% $blowjobs) {
echo 'Oh yea that feels so good, don't stop';
}
?>
immitestawl
June 12, 2011, June 12, 2011 04:27, permalink
must check and check coupon code available
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