What does FILTER_SANITIZE_STRING do?

What does FILTER_SANITIZE_STRING do?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

What Does FILTER_SANITIZE_STRING Do?

There are countless Stack Overflow threads dissecting FILTER_FLAG_STRIP_LOW, FILTER_FLAG_ENCODE_INVALID, and every other flag in the book. They explain how to strip non-ASCII characters or force UTF-8 encoding. But what happens when you call filter_var() with just FILTER_SANITIZE_STRING and no flags? Does it simply filter HTML tags like some people assume, or is there more going on under the hood?

The short answer is that FILTER_SANITIZE_STRING does not strip HTML tags by default. If your goal is to remove <script> blocks or convert a block of HTML into plain text, relying solely on this flag will leave you with unescaped HTML entities instead of clean text. In fact, without any flags, it behaves almost exactly like htmlspecialchars() in its core intent: it converts special characters into their corresponding HTML entities.

The Core Mechanism

When you invoke filter_var('Hello <World>', FILTER_SANITIZE_STRING), the function returns 'Hello &lt;World&gt;'. It is essentially a wrapper around htmlspecialchars() that uses ENT_QUOTES and UTF-8 encoding by default. This behavior has been consistent since PHP 5.3, though the specific implementation details and performance characteristics have evolved slightly over versions.

The primary purpose of this filter is to sanitize strings for displaying them in HTML contexts. It prevents XSS (Cross-Site Scripting) attacks caused by special characters breaking out of your output buffer. However, because it doesn't strip tags, you must be careful not to confuse "escaping" with "stripping."

When You Should Use It

You should use FILTER_SANITIZE_STRING whenever you are about to echo a user-provided value into an HTML document without intending to render the HTML within that string as actual markup.

Consider a comment form where users can post text:

$comment = $_POST['message'];

// Safe for display in <div class="comment">
echo '<div class="comment">' . filter_var($comment, FILTER_SANITIZE_STRING) . '</div>';

In this scenario, if a user submits <script>alert('XSS')</script>, the output becomes:

<div class="comment">&lt;script&gt;alert('XSS')&lt;/script&gt;</div>

The browser renders this as safe text rather than executing the script. This is the intended use case for the default flag set.

The Trap of Tag Stripping

A common misconception arises because older tutorials or vague documentation sometimes imply that FILTER_SANITIZE_STRING cleans up HTML. It does not strip tags unless you explicitly tell it to via flags. If you need to remove tags entirely, you must combine the filter with FILTER_FLAG_STRIP_HTML.

$cleanText = filter_var($input, FILTER_SANITIZE_STRING | FILTER_FLAG_STRIP_HTML);
// Returns: "Hello World" (strips <World>)

However, adding FILTER_FLAG_STRIP_HTML changes the behavior significantly. It removes all HTML tags but keeps their content. This is often what developers actually want when they ask "how do I remove HTML tags?", even if they forget to specify the flag initially.

Trade-offs and Modern Alternatives

While FILTER_SANITIZE_STRING is useful for legacy codebases or quick sanitization needs, it has limitations in modern development workflows. The function does not strip whitespace, null bytes, or control characters unless you add specific flags like FILTER_FLAG_STRIP_NULL_BYTES.

Furthermore, relying solely on PHP's built-in filters can lead to subtle bugs if the input is intended for JSON output instead of HTML. If you are serializing data for an API response, using htmlspecialchars with ENT_QUOTES and UTF-8 is still the standard, but you must ensure you aren't accidentally double-encoding strings that were already escaped.

For complex scenarios involving rich text editors or CMS platforms, the distinction between "escaping for HTML" and "stripping tags" becomes critical. If you are building a system where users submit content that should be stored as plain text but displayed with formatting later, using FILTER_FLAG_STRIP_HTML during storage ensures your database remains clean of markup, while leaving it unset (or just using FILTER_SANITIZE_STRING) during display ensures safety in the browser.

If you are working within the Laravel ecosystem, remember that the framework's e() helper function essentially wraps this logic for you, automatically escaping variables intended for HTML output without needing to manually invoke filter_var(). However, understanding the underlying mechanics of FILTER_SANITIZE_STRING helps when debugging edge cases or handling raw inputs in services and tasks where Blade templating isn't involved.

Ultimately, FILTER_SANITIZE_STRING is a tool for encoding, not cleaning up markup. Knowing whether you need to escape characters or strip tags determines which flags you must combine with the default filter.

Our network