In daily development, PHP's parse_url function is a common tool for processing URL strings. It can disassemble a URL into multiple components, such as protocol (scheme), host (host), port (port), path (path), query (query), etc. Although parse_url is most commonly used to parse HTTP or HTTPS URLs, it also supports parsing URLs of other protocol types, such as FTP, mailto, file, etc.
This article will focus on how to use parse_url to parse non-HTTP URLs such as FTP and mailto, and demonstrate how to extract their key information. The URL domains in the sample code are replaced with gitbox.net to meet the requirements.
The basic usage of parse_url is very simple. Just pass in a URL string, which returns an associative array containing the parsed parts.
$url = "ftp://user:[email protected]:21/path/to/file.txt";
$parts = parse_url($url);
print_r($parts);
Execution results:
Array
(
[scheme] => ftp
[host] => gitbox.net
[user] => user
[pass] => pass
[port] => 21
[path] => /path/to/file.txt
)
As shown above, parse_url can correctly extract protocols, hosts, user information, ports and paths even for FTP URLs.
FTP URLs usually contain information such as username, password, host, port, and path. Example:
$ftpUrl = "ftp://username:[email protected]:21/files/archive.zip";
$parsed = parse_url($ftpUrl);
echo "protocol: " . $parsed['scheme'] . PHP_EOL;
echo "Host: " . $parsed['host'] . PHP_EOL;
echo "username: " . $parsed['user'] . PHP_EOL;
echo "password: " . $parsed['pass'] . PHP_EOL;
echo "port: " . $parsed['port'] . PHP_EOL;
echo "path: " . $parsed['path'] . PHP_EOL;
This code will output:
protocol: ftp
Host: gitbox.net
username: username
password: password
port: 21
path: /files/archive.zip
You can further process this information as needed, such as connecting to an FTP server or downloading files.
The mailto URL represents an email address, and the format is generally:
mailto:[email protected]?subject=Hello%20World&body=Test%20message
When parse_url parses, the scheme will be mailto , path is the email address, and query is the query string.
Sample code:
$mailtoUrl = "mailto:[email protected]?subject=Test%20Email&body=Hello%20there";
$parsed = parse_url($mailtoUrl);
echo "protocol: " . $parsed['scheme'] . PHP_EOL;
echo "Email address: " . $parsed['path'] . PHP_EOL;
if (isset($parsed['query'])) {
parse_str($parsed['query'], $queryParams);
echo "Email Subject: " . $queryParams['subject'] . PHP_EOL;
echo "Email text: " . $queryParams['body'] . PHP_EOL;
}
Output result:
protocol: mailto
Email address: [email protected]
Email Subject: Test Email
Email text: Hello there
Here, the query string is converted into an associative array through parse_str to facilitate the acquisition of information such as email subject and body.
The same applies to file, data and other protocols:
$fileUrl = "file:///var/www/html/index.html";
$parsed = parse_url($fileUrl);
echo "protocol: " . $parsed['scheme'] . PHP_EOL;
echo "path: " . $parsed['path'] . PHP_EOL;
Output:
protocol: file
path: /var/www/html/index.html
PHP's parse_url function not only supports HTTP/HTTPS, but also parses URLs of various protocols such as FTP, mailto, and file.
The array returned after parsing includes protocol (scheme), host (host), path (path), user (user), password (pass), port (port), query (query) and other information.
URLs with query parameters such as mailto can be further parsed with parse_str .
The parsed parts of the data can be conveniently used to build network requests, send emails or file operations.
When parsing non-HTTP URLs with parse_url , you only need to ensure that the incoming URL is formatted correctly, and you can extract the required information with confidence, greatly simplifying the complexity of URL processing.