What is the purpose of using a regular expression in PHP to extract a specific part of a URL?
When working with URLs in PHP, you may need to extract a specific part of the URL, such as the domain name or query parameters. Regular expressions can be used to efficiently extract this information from a URL string. By using a regular expression, you can easily target and extract the desired part of the URL without having to manually parse the string.
$url = "https://www.example.com/page?param1=value1&param2=value2";
// Extract domain name from URL
preg_match('/^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n?]+)/', $url, $matches);
$domain = $matches[1];
echo $domain; // Output: example.com