← Home ← Codex ← DEBT ← Engine
Browse by Category
+ added · updated 7d
← Back to glossary

strip_tags()

PHP PHP 5.0+ Intermediate
debt(d5/e3/b3/t9)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The detection_hints list semgrep and phpstan — both specialist SAST/static-analysis tools — as the means to catch the pattern of strip_tags() used as sole XSS protection or with allowed tags lacking attribute sanitisation. Default linters would not flag this, and it is silent at runtime until an attacker exploits it.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix indicates replacing strip_tags() with htmlspecialchars() for HTML output, or combining both for plain-text output. This is a targeted find-and-replace of the sanitisation call at each output point, touching potentially a few files but not a cross-cutting architectural change.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The misuse is typically concentrated in output/view layers or specific input-handling routines. It applies to web and cli contexts but does not define the system's shape; fixing it is scoped to the places where user content is echoed, not a codebase-wide structural commitment.

t9 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'catastrophic trap — the obvious way is always wrong' (t9). The misconception field states it directly: developers believe strip_tags() prevents XSS, but event-handler attributes on allowed tags still execute JavaScript. The function's name and common usage strongly imply safety, yet the 'obvious' usage pattern (stripping tags to sanitise output) is always wrong as a standalone XSS defence — exactly matching the t9 anchor.

About DEBT scoring →

Also Known As

strip_tags() PHP HTML strip tag removal

TL;DR

Removes HTML and PHP tags from a string — not a reliable XSS defence on its own.

Explanation

strip_tags($string) removes all HTML and PHP tags from a string. It is often misused as an XSS prevention mechanism, but it is not reliable for this purpose — it can be bypassed with malformed tags, and it strips tags rather than encoding them, destroying legitimate content. The correct defence against XSS in HTML output is htmlspecialchars(). Use strip_tags() only when you genuinely want to remove markup — such as sanitising input for plain-text storage — and never as a substitute for output encoding.

Common Misconception

strip_tags() prevents XSS. strip_tags() removes tags but leaves attribute-based payloads intact — an event handler like onclick="..." on an allowed tag still executes. For XSS prevention, use context-aware output encoding (htmlspecialchars), not tag stripping.

Why It Matters

strip_tags() removes HTML tags but leaves attributes — allowing <p onmouseover='...'> through, meaning it is not a safe XSS defence when applied to allowed tags.

Common Mistakes

  • Using strip_tags() as an XSS defence — event attributes in allowed tags still execute JavaScript.
  • Passing an allowlist of tags without sanitising the attributes on those allowed tags.
  • Not using htmlspecialchars() after strip_tags() — the output still needs context-appropriate escaping.
  • Using strip_tags() to clean user input for database storage — the issue is output encoding, not stripping.

Avoid When

  • Never rely on strip_tags() as your sole XSS prevention strategy — encoded payloads, SVG, and CSS survive tag stripping.
  • Do not use strip_tags() to sanitise input for HTML output — use htmlspecialchars() or an HTML purifier library instead.

When To Use

  • Use strip_tags() only when you need to remove HTML tags from content for plain-text display — not as an XSS defence.
  • Combine strip_tags() with htmlspecialchars() when displaying user content in a plain-text context.

Code Examples

✗ Vulnerable
// strip_tags() as XSS defence — easily bypassed
$input = strip_tags($_POST['bio']);
echo $input; // <scr<script>ipt>alert(1)</scr</script>ipt> survives
✓ Fixed
// strip_tags() — useful for producing plain text, not for XSS prevention
$plain     = strip_tags($htmlContent);          // strip for plain-text email
$wordCount = str_word_count(strip_tags($body));  // word count

// For PLAIN text output — use htmlspecialchars():
echo htmlspecialchars($userInput, ENT_QUOTES|ENT_SUBSTITUTE, 'UTF-8');

// For RICH text output — use HTMLPurifier:
$purifier = new HTMLPurifier(HTMLPurifier_Config::createDefault());
$clean    = $purifier->purify($_POST['description']);
echo $clean;

Added 15 Mar 2026
Edited 31 Mar 2026
Views 45
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 1 ping F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 2 pings T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 1 ping T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F
No pings yet today
No pings yesterday
Amazonbot 9 Ahrefs 4 Google 3 ChatGPT 3 PetalBot 3 Perplexity 2 Unknown AI 2 Bing 2 Scrapy 2 Meta AI 1 Twitter/X 1
crawler 29 crawler_json 3
🧱 FUNDAMENTALS — new to this? Start with the ground floor.
PHP php A server-side scripting language that generates web pages and APIs — the code runs on the server, and only its output (usually HTML or JSON) reaches the browser.

PHP is often the first server-side language people meet, and understanding its execution model — script starts fresh on every request, no memory between requests — explains most of how the web backend works: sessions, databases, and caching all exist to bridge that per-request amnesia.

💡 Start with PHP 8.x, declare(strict_types=1), and PDO — skip any tutorial that mentions mysql_query().

Ask Codex about PHP →
DEV INTEL Tools & Severity
🟠 High ⚙ Fix effort: Low
⚡ Quick Fix
Use strip_tags() to remove HTML for plain-text output, not as an XSS defence — use htmlspecialchars() for HTML output or DOMPurify (JS) for rich HTML; strip_tags() has bypasses
📦 Applies To
PHP 5.0+ web cli
🔗 Prerequisites
🔍 Detection Hints
strip_tags() used as sole XSS protection before echoing HTML; no htmlspecialchars() on output; strip_tags() with allowed tags still vulnerable
Auto-detectable: ✓ Yes semgrep phpstan
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Medium Context: Line Tests: Update
CWE-79


✓ schema.org compliant