views:

77

answers:

6

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
Why the downvote?
Franz
A: 

You could just use substr to check if the string begins with 'http://' and remove that part; no regex.

Jeff
+4  A: 

I think maybe a regular expression would be overkill here. How about this?

$str = str_replace(array('http://', 'https://'),'',$str);
Eric Petroelje
agreed...no need to check if it exists...just replace it.
Galen
+1. this is actually better than mine. And even better, if you make it `array('http://', 'https://')`.
Franz
@Franz - thanks, and good idea. I updated my answer.
Eric Petroelje
http:// could be occur twice or more times in a url!
powtac
what about ftp://, do you happen to have a full list of every valid protocol?
tloach
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
@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
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
+1 for not writing his code for him.
Jed Smith
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
+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