0

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?

4

5 Answers 5

1

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);
Sign up to request clarification or add additional context in comments.

7 Comments

This isn't answering the question. The user said: Is there a way I can just remove everything before the domain name, no matter what it is?
This answers the problem of how to remove http and https, and gives a good example of how to quickly add more protocols.
@kirk while those are his words, the intent of the user seems to be just to remove http and https.
@JAL: Thank you, at least you saw the same thing...
@NeoNexusDeMortis I very much do see that, but being a part of this community we want to provide complete answers. Not just whatever comes to mind, and your second answer does not work on techcrunch.com/startups/?redirect=https://test.com. It just returns test.com
|
0

Something like this ought to do:

$url = preg_replace("|^.+?://|", "", $url);

Removes everything up to and including the ://

2 Comments

@Glavić: good catch! fixed it. with a non-greedy + it'll only catch up to the first ://
0

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

1 Comment

0
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

Comments

-1
$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().

8 Comments

String replacement in this fashion is not a good idea. You are assuming that all users will always enter URLS into the input correctly. It will only take one user to enter a URL like "http:///www.amazon.com" to break this code. Always assume users have entered invalid code, and write the most robust code possible
What if user enters url like: techcrunch.com/startups/?redirect=https://test.com ?
@SlyRaskal I solved your problem, now to think about Glavics'.
Also where did you pull that 80% number from? Seriously, it's nothing but made up.
I'm not picking on you. I'm critiquing your code. That's what this site is all about. If you do not wish to listen, and think about the guidance of more experienced programmers, you're in for a world of hurt in the programming sector.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.