What are the best practices for handling user input containing HTML links in PHP?

When handling user input containing HTML links in PHP, it is important to sanitize the input to prevent any potential security vulnerabilities such as cross-site scripting (XSS) attacks. One way to do this is by using the `htmlspecialchars()` function to encode any HTML entities in the input before displaying it on a webpage.

$userInput = "<a href='http://example.com'>Click here</a>"; // User input containing HTML link

$sanitizedInput = htmlspecialchars($userInput, ENT_QUOTES, 'UTF-8'); // Sanitize the input

echo $sanitizedInput; // Display the sanitized input on the webpage