strip_tags() removes HTML elements but fails to sanitize inline attributes or malformed HTML payload vectors. Use HTMLPurifier for rich text input.
A common security misconception in PHP is relying on strip_tags() to sanitize user-submitted rich text. It allows attribute payloads like onload= or javascript: URIs through if allowed tags are specified.
use HTMLPurifier;
$purifier = new HTMLPurifier();
$cleanHtml = $purifier->purify($input);
- strip_tags() does not validate tag attributes or execution vectors
- Always escape plain text with e() or Blade's {{ $var }}
- For user-submitted rich text, use reliable HTML sanitizers like HTMLPurifier
Related Tips
View all tips →Never Return Statements inside Finally Blocks in PHP
Placing a return statement inside a try-catch finally block silently overrides exceptions and previous return statements.
Reduce Indentation with Early Returns and Guard Clauses
Replace deeply nested if statements with guard clauses that exit functions early when preconditions fail.