Deprecated: Constant FILTER_SANITIZE_STRING is deprecated
Deprecated: Constant FILTER_SANITIZE_STRING is deprecated in /var/www/app/form.php on line 12Short answer
PHP 8.1 deprecated FILTER_SANITIZE_STRING and its alias FILTER_SANITIZE_STRIPPED. Replace each use by what you actually need: htmlspecialchars when the value is printed in HTML, or strip_tags when you want the tags removed before storing it. There is no one-line drop-in, because the old output cannot be matched exactly.
Fix it fast
1Find out which cause you have
List every place the constants are used, so you replace each one by its purpose and not by a blind search and replace.
grep -rn -E "FILTER_SANITIZE_(STRING|STRIPPED)|FILTER_FLAG_NO_ENCODE_QUOTES" --include=*.php .2Match what you see, then jump to its fix
What does the error look like?
Deprecated: Constant FILTER_SANITIZE_STRING is deprecated in /var/www/app/form.php on line 12Deprecated: Constant FILTER_SANITIZE_STRIPPED is deprecated
Cause 1 of 2
The value is printed back into an HTML page
1Check
Find where the value ends up. If it is echoed into HTML, escaping at output is the job, not stripping on input.
grep -rn "FILTER_SANITIZE_STRING" --include=*.php . | grep -E "echo|print"2Fix
Escape on output with htmlspecialchars and name the encoding. Since PHP 8.1 the defaults convert both quote types and replace invalid UTF-8 instead of returning an empty string.
echo htmlspecialchars($_POST['name'], ENT_QUOTES, 'UTF-8');3Verify
Post a value with a tag and a quote in it. The page should show them as text.
php -r 'echo htmlspecialchars("<b>Tom</b> & \"Jerry\"", ENT_QUOTES, "UTF-8");'Confirmed by php.net
Cause 2 of 2
The value should lose its tags before it is stored
1Check
The old filter stripped tags. strip_tags is the matching function. It is not an XSS defence: allowed tags keep all their attributes, and it strips only what looks like a tag.
grep -rn "FILTER_SANITIZE_STRING" --include=*.php . | grep -i -E "insert|save|store|update"2Fix
Use strip_tags for the tag removal, keep the value in a prepared statement for the database, and still escape on output.
$title = strip_tags($_POST['title']);3Verify
Pass a string with a tag through it and check what is left.
php -r 'echo strip_tags("<p>Hello <b>world</b></p>");'Confirmed by php.net
How do I stop it coming back?
- Escape for the place the data goes: htmlspecialchars for HTML, prepared statements for SQL.
- Check input against the type the field must be and reject what does not match.
- Run the test suite with error_reporting set to E_ALL so deprecations show up before an upgrade.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix