How can input validation be implemented to restrict user input to URLs starting with a specific pattern in PHP?

To restrict user input to URLs starting with a specific pattern in PHP, you can use regular expressions to validate the input. By defining a regex pattern that matches the desired URL format, you can ensure that only URLs starting with that specific pattern are accepted as valid input.

// Define the regex pattern for URLs starting with a specific pattern
$pattern = '/^https:\/\/example.com/';

// Get user input
$userInput = $_POST['url'];

// Validate the user input against the regex pattern
if (preg_match($pattern, $userInput)) {
    // Input is valid
    echo "Valid URL input: " . $userInput;
} else {
    // Input is invalid
    echo "Invalid URL input. Please enter a URL starting with 'https://example.com'.";
}