filter_var()
` function along with a suitable filter. Here's an example :<?php
$string = "<script>alert('Hack!');</script>";
$sanitized_string = filter_var($string, FILTER_SANITIZE_STRING);
echo $sanitized_string;
?>
alert('Hack!');
FILTER_SANITIZE_STRING
` filter is used to remove all HTML tags and characters with special meaning in HTML from the string. The resulting sanitized string only contains plain text. filter_var()
` function as per your requirement. You can also create custom filters if the existing ones do not meet your needs.