How can regular expressions be optimized for better handling of URLs without "http://" in PHP?

Regular expressions can be optimized for better handling of URLs without "http://" in PHP by using a more specific pattern that matches URLs starting with "www." or any other valid URL prefix. This can be achieved by modifying the regular expression pattern to include optional prefixes such as "www." or "https://". By doing so, the regular expression will be more accurate in identifying URLs without "http://" and can effectively parse and handle them in PHP.

// URL without "http://" regular expression pattern
$pattern = '/^(?:(?:https?:\/\/)?(?:www\.)?)([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/';

$url = "www.example.com"; // Example URL without "http://"

if (preg_match($pattern, $url, $matches)) {
    $domain = $matches[1];
    $extension = $matches[2];
    
    echo "Domain: $domain\n";
    echo "Extension: $extension\n";
}