How can PHP developers effectively validate and sanitize URL parameters to prevent manipulation by malicious users?

To prevent manipulation by malicious users, PHP developers can validate and sanitize URL parameters by using functions like filter_var() to ensure the input is a valid URL and htmlentities() to sanitize the input and prevent XSS attacks.

// Validate and sanitize URL parameter
$url = filter_var($_GET['url'], FILTER_VALIDATE_URL);
if($url){
    $sanitized_url = htmlentities($url);
    // Use $sanitized_url in your code
} else {
    // Handle invalid URL input
}