拿网址并解析其中的一部分[重复]

Possible Duplicate:
Getting domain name without TLD

So I have a Web site I am trying to work on. In the URL there is info and I want to parse it out and use it to make a db call to mysql:

http://www.relevant-info.org/home.html

I want to take the Url put it into a string.

Then I want to only get 'relevant-info' out of the url.

$webaddress = $_SERVER["SERVER_NAME"];
$parts = parse_url($webaddress);
$url = substr($Parts, 4, -4);

^ i am using that and I got the ip address (it is a centos 5.8 lamp) on a VM i am testing this, so in short this seams to work good so far :)

Credits to Relentless

You can easily find it with the PHP parse-url function: parse_url

Example usage: If you want to get 'relevant-info', a PHP 5.4 valid code could look like:

$Url = substr(parse_url("http://www.relevant-info.org/home.html")[1], 4, -4);

Output

relevant-info

For PHP 5.3 and less

$Parts = parse_url("http://www.relevant-info.org/home.html");
$Url = substr($Parts, 4, -4);

In future situtations, if you do not have fancy functions like parse-url, you can use Regex or use str_split

Remember that str_split requires less resources

parse_url won't help, because it doesn't split the domain.

I would suggest a regex. Depending wether you are using only www subdomains something like:

preg_match(#http(s)?://(www.)?(.+?)\..*#, $subject, $matches)

$matches[3] should contain the subdomain of the TLD

I have another great idea!

What about explode?

var_dump(explode('.', 'http://www.relevant-info.org/home.html'));
array(4) {
  [0]=>
  string(10) "http://www"
  [1]=>
  string(13) "relevant-info"
  [2]=>
  string(8) "org/home"
  [3]=>
  string(4) "html"
}