I have a PHP script that removes the "http://" from user input url strings.
My Script:
$url= "http://techcrunch.com/startups/"; $url = str_replace('http://', '', $url);
Result:
$url= techcrunch.com/startups/
This works great, except that sometimes urls have "https://" instead. Is there a way I can just remove everything before the domain name, no matter what it is?
This question is related to
php
Use look behinds in preg_replace
to remove anything before //
.
preg_replace('(^[a-z]+:\/\/)', '', $url);
This will only replace if found in the beginning of the string, and will ignore if found later
Something like this ought to do:
$url = preg_replace("|^.+?://|", "", $url);
Removes everything up to and including the ://
preg_replace('/^[^:\/?]+:\/\//','',$url);
some results:
input: http://php.net/preg_replace output: php.net/preg_replace input: https://www.php.net/preg_replace output: www.php.net/preg_replace input: ftp://www.php.net/preg_replace output: www.php.net/preg_replace input: https://php.net/preg_replace?url=http://whatever.com output: php.net/preg_replace?url=http://whatever.com input: php.net/preg_replace?url=http://whatever.com output: php.net/preg_replace?url=http://whatever.com input: php.net?site=http://whatever.com output: php.net?site=http://whatever.com
Try this out:
$url = 'http://techcrunch.com/startups/'; $url = str_replace(array('http://', 'https://'), '', $url);
EDIT:
Or, a simple way to always remove the protocol:
$url = 'https://www.google.com/'; $url = preg_replace('@^.+?\:\/\/@', '', $url);
$new_website = substr($str, ($pos = strrpos($str, '//')) !== false ? $pos + 2 : 0);
This would remove everything before the '//'.
EDIT
This one is tested. Using strrpos()
instead or strpos()
.
Source: Stackoverflow.com