-
-
- $search = '~^(([^:/?#]+):)?(//([^/?#]*))?( [^?#]*)(?([^#]*))?(#(.*))?~i';
- $url = 'http://www.php.net/pub/ietf/uri /#Related';
- $url = trim($url);
- preg_match_all($search, $url ,$rr);
- printf("
The output URL data is: %s n",var_export( $rr ,TRUE));
/*
- Each group is as follows
- $1 = http:
- $2 = http
- $3 = //www. php.net
- $4 = www.php.net
- $5 = /pub/ietf/uri/
- $6 =
- $7 =
- $8 = #Related
- $9 = Related
- */
- ?> ;
-
Copy the code
Another concise code is provided here:
-
- // Get the hostname from the URL
- preg_match("/^(http ://)?([^/]+)/i", "http://www.php.net/index.html", $matches);
- $host = $matches[2];
- // from Get the last two segments from the host name
- preg_match("/[^./]+.[^./]+$/", $host, $matches);
- echo "domain name is: {$matches[0]} n";
- ?>
Copy the code
Output after execution: domain name is: php.net
|