-
-
- $search = '~^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(?([^#]*))?(#(.*))?~i';
- $url = 'http://www.php.net/pub/ietf/uri/#Related';
- $url = trim($url);
- preg_match_all($search, $url ,$rr);
- printf("
输出URL数据为: %s n",var_export( $rr ,TRUE));
/*
- 各分组如下
- = http:
- = http
- = //www.php.net
- = www.php.net
- = /pub/ietf/uri/
- =
- =
- = #Related
- = Related
- */
- ?>
-
复制代码
这里提供另一个简洁的代码:
-
- // 从 URL 中取得主机名
- preg_match("/^(http://)?([^/]+)/i", "http://www.php.net/index.html", $matches);
- $host = $matches[2];
- // 从主机名中取得后面两段
- preg_match("/[^./]+.[^./]+$/", $host, $matches);
- echo "domain name is: {$matches[0]}n";
- ?>
复制代码
执行后输出:domain name is: php.net
|