Latest web development tutorials
 

PHP FILTER_SANITIZE_STRING Filter

< PHP Filter Reference

Example

Remove all HTML tags from a string:

<?php
$str = "<h1>Hello World!</h1>";

$newstr = filter_var($str, FILTER_SANITIZE_STRING);
echo $newstr;
?>
Run example »

Definition and Usage

The FILTER_SANITIZE_STRING filter removes tags and remove or encode special characters from a string.

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 &amp;

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;
?>
Run example »

< PHP Filter Reference