I have a form where users enter a URL. I need a regular expression to skip the part 'http://' if present. For example, if the user insert into the form: http://youtube.com I need to save only youtube.com. I don't know regular expressions. Could you help me? Thanks!
A:
You do not really need regular expressions. This would be enough:
if (substr($str, 0, 7) == 'http://')
$str = substr($str, 7);
else if (substr($str, 0, 8) == 'https://')
$str = substr($str, 8);
EDIT: Added the else if
part.
Franz
2009-11-18 19:38:41
Why the downvote?
Franz
2009-11-18 19:47:14
A:
You could just use substr to check if the string begins with 'http://' and remove that part; no regex.
Jeff
2009-11-18 19:39:03
+4
A:
I think maybe a regular expression would be overkill here. How about this?
$str = str_replace(array('http://', 'https://'),'',$str);
Eric Petroelje
2009-11-18 19:39:43
+1. this is actually better than mine. And even better, if you make it `array('http://', 'https://')`.
Franz
2009-11-18 19:43:44
what about ftp://, do you happen to have a full list of every valid protocol?
tloach
2009-11-18 19:47:58
unless he wants to avoid stripping it out in the case of a string like 'example string that contains http:// in the middle of it because it is a comment explaining the syntax of a URL'.
Brian Schroth
2009-11-18 19:53:02
@powtac - yeah, I thought about that, but thinking about the context of the question, a URL with an http:// in the query string would probably be some sneaky attempt at redirection and is likely unwanted anyways.
Eric Petroelje
2009-11-18 19:59:56
A:
you could do it without a regex, since you don't know regexes, and just check if the index of the first found "http://" string (case insensitive) is at position 0, and if it is remove it.
Brian Schroth
2009-11-18 19:40:04
A:
Not needed. However, if you want regex:
// get host name from URL
preg_match('@^(?:http://)?([^/]+)@i', $the_url, $matches);
$host = $matches[1];
David Elizondo
2009-11-18 19:51:37
+1
A:
Please keep in mind, that the string http://
could occur twice or more times in an URL!
$parts = parse_url('http://youtube.com');
$url = $parts['host'].$parts['path']; //.'?'.$parts['query'].'#'.$parts['fragment']
See parse_url()
powtac
2009-11-18 19:52:49