What are some alternative methods to removing HTML tags from user input in PHP besides using the strip_tags function?

When dealing with user input in PHP, it's important to remove HTML tags to prevent potential security vulnerabilities such as cross-site scripting (XSS) attacks. One common method to achieve this is by using the strip_tags function, which removes all HTML and PHP tags from a string. However, if you're looking for alternative methods, you can also use regular expressions or the htmlspecialchars function to sanitize user input.

// Using regular expressions to remove HTML tags from user input
$userInput = "<p>Hello, <strong>world</strong>!</p>";
$cleanInput = preg_replace('/<[^>]*>/', '', $userInput);
echo $cleanInput;

// Using htmlspecialchars to encode HTML tags in user input
$userInput = "<p>Hello, <strong>world</strong>!</p>";
$cleanInput = htmlspecialchars($userInput, ENT_QUOTES, 'UTF-8');
echo $cleanInput;