Forum Moderators: coopster
The following code should output www.php.net:
<?php
preg_match('@^(?:http://)?([^/]+)@i',
"http://www.php.net/index.html", $matches);
$host = $matches[1];
echo $host;
?>
If someone could explain the all the details of how it works I would really be grateful.
Thanks.
@^(?:http://)?([^/]+)@i. There are numerous rules to making these patterns, so perhaps it's best that you read the documentation on this and ask specific questions that I'll be happy to answer for you.
You can find the pattern syntax here: [php.net...]
Hope that gets you started. :)
Why does $matches[1] gets the value of the question mark in between?
Thanks.
Ok,so "http://" doesn't count because it starts with ?: (right?)
Correct.
Why does $matches[1] gets the value of the question mark in between?
It's not the question mark in between, that is just telling the regular expression parser that the http:// is optional. The next set of capturing subpattern is
([^/]+), which is telling the engine to capture one or more of anything that is not a slash character.
Basically you are grabbing the host/domain portion of the uri.