|
The parse_url () function can parse the URL and return its components. Its usage is as follows: Array parse_url (string url)
This function returns an associated array that contains various components of an existing URL. If one of them is missing, an array item is not created for this component. Components:
- Scheme-such as http
- Host
- Port
- User
- Pass
- Path
- Query-question mark? After
- Fragment-after the hash symbol #
This function does not mean that the given URL is valid. it only separates the parts in the list above. Parse_url () can accept incomplete URLs and resolve them correctly as much as possible. This function does not work for URLs with relative paths.
- $ Url = "http://www.nowamagic.net/welcome ";
- $ Parts = parse_url ($ url );
- Print_r ($ parts );
- ?>
The program running result is as follows:
- Array ([scheme] => http [host] => www.nowamagic.net [path] =>/welcome /)
- $ Url = 'http: // username: password @ hostname/path? Arg = value # anchor ';
- Print_r (parse_url ($ url ));
- Echo parse_url ($ url, PHP_URL_PATH );
- ?>
Program output:
- Array
- (
- [Scheme] => http
- [Host] => hostname
- [User] => username
- [Pass] => password
- [Path] =>/path
- [Query] => arg = value
- [Fragment] => anchor
- )
- /Path
As you can see, it is easy to break down all parts of a URL. it is also easy to take the specified part, for example:
- Echo parse_url ($ url, PHP_URL_PATH );
In the second parameter, set the following parameters: PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY or PHP_URL_FRAGMENT
|