How can string manipulation functions like strpos() and substr() be utilized effectively in PHP for extracting specific parts of a URL?

When dealing with URLs in PHP, string manipulation functions like strpos() and substr() can be used effectively to extract specific parts of the URL. For example, you can use strpos() to find the position of a specific substring in the URL and then use substr() to extract a portion of the URL based on that position.

$url = "https://www.example.com/blog/article";
$protocol = substr($url, 0, strpos($url, "://")); // Extract protocol (https)
$domain = substr($url, strpos($url, "://") + 3, strpos($url, "/", strpos($url, "://") + 3) - strpos($url, "://") - 3); // Extract domain (www.example.com)
$path = substr($url, strpos($url, "/", strpos($url, "://") + 3)); // Extract path (/blog/article)

echo "Protocol: " . $protocol . "\n";
echo "Domain: " . $domain . "\n";
echo "Path: " . $path . "\n";