Google News
logo
PHP Program to Sanitize a string
To sanitize a string in PHP, you can use the `filter_var()` function along with a suitable filter. Here's an example :
Program :
<?php
$string = "<script>alert('Hack!');</script>";
$sanitized_string = filter_var($string, FILTER_SANITIZE_STRING);
echo $sanitized_string;
?>
Output :
alert('Hack!');
In the above code, the `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.

You can use other filters from the `filter_var()` function as per your requirement. You can also create custom filters if the existing ones do not meet your needs.