How can the substr() function be used effectively to check for specific patterns in URLs in PHP?

When working with URLs in PHP, you may need to check for specific patterns or extract certain parts of the URL. The substr() function can be used effectively to achieve this by extracting a portion of the URL based on the starting position and length. By using substr() in combination with other string manipulation functions, you can easily check for specific patterns in URLs.

$url = "https://www.example.com/page1";
$domain = substr($url, 8, strpos($url, "/", 8) - 8); // Extract the domain from the URL

if ($domain == "www.example.com") {
    echo "URL matches the specified domain pattern";
} else {
    echo "URL does not match the specified domain pattern";
}