How can functions be utilized in PHP to simplify the process of checking and filtering links based on predefined criteria?

To simplify the process of checking and filtering links based on predefined criteria in PHP, we can create a custom function that takes a link as input and applies the criteria to determine if the link should be allowed or not. This function can be reused throughout the codebase to easily filter links according to the specified rules.

function filterLink($link) {
    // Define your criteria for filtering links here
    $allowedDomains = ['example.com', 'google.com'];
    
    // Check if the link meets the criteria
    $parsedUrl = parse_url($link);
    if (isset($parsedUrl['host']) && in_array($parsedUrl['host'], $allowedDomains)) {
        return true; // Link meets the criteria
    } else {
        return false; // Link does not meet the criteria
    }
}

// Example usage
$link = 'https://example.com/page';
if (filterLink($link)) {
    echo "Link is allowed";
} else {
    echo "Link is not allowed";
}