PHP FILTER_SANITIZE_STRING Filter
Example
Remove all HTML tags from a string:
<?php
$str = "<h1>Hello World!</h1>";
$newstr = filter_var($str, FILTER_SANITIZE_STRING);
echo $newstr;
?>
Try it Yourself »
Definition and Usage
The FILTER_SANITIZE_STRING filter removes tags and remove or encode special characters from a string.
The FILTER_SANITIZE_STRING filter was deprecated in PHP 8.1.0. Use htmlspecialchars() to encode special characters, or strip_tags() to remove HTML tags.
Possible options and flags:
- FILTER_FLAG_NO_ENCODE_QUOTES - Do not encode quotes
- FILTER_FLAG_STRIP_LOW - Remove characters with ASCII value < 32
- FILTER_FLAG_STRIP_HIGH - Remove characters with ASCII value > 127
- FILTER_FLAG_ENCODE_LOW - Encode characters with ASCII value < 32
- FILTER_FLAG_ENCODE_HIGH - Encode characters with ASCII value > 127
- FILTER_FLAG_ENCODE_AMP - Encode the "&" character to &
More Examples
Example 1
Remove all HTML tags and all characters with ASCII value > 127, from a string:
<?php
$str = "<h1>Hello WorldÆØÅ!</h1>";
$newstr = filter_var($str, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
echo $newstr;
?>
Try it Yourself »
❮ PHP Filter Reference
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.