-
-
$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));
/*
- 各分组如下
- $1 = http:
- $2 = http
- $3 = //www.php.net
- $4 = www.php.net
- $5 = /pub/ietf/uri/
- $6 =
- $7 =
- $8 = #Related
- $9 = 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
|