strip_tags
strip_tags(string $string, array|string|null $allowed_tags = null): string
Strips HTML and PHP tags from a string (with an optional whitelist of tags to keep).
C C23
#include <stdio.h>
// finite state machine: everything between '<' and '>' is dropped
int main(void) {
const char *s = "<p>Hello <b>World</b></p>";
int in_tag = 0;
for (const char *p = s; *p; p++) {
if (*p == '<') in_tag = 1;
else if (*p == '>') in_tag = 0;
else if (!in_tag) putchar(*p);
}
printf("\n");
return 0;
}
Hello World
C++ C++26
#include <iostream>
#include <string>
// finite state machine: drop between '<' and '>' (same idea as the C version)
std::string strip_tags(const std::string &s) {
std::string out;
bool in_tag = false;
for (char c : s) {
if (c == '<') in_tag = true;
else if (c == '>') in_tag = false;
else if (!in_tag) out += c;
}
return out;
}
int main() {
std::cout << strip_tags("<p>Hello <b>World</b></p>") << "\n";
return 0;
}
Hello World
C# v11.0.100
using System.Text.RegularExpressions;
Console.WriteLine(Regex.Replace("<p>Hello <b>World</b></p>", "<[^>]+>", ""));
Hello World
Go v1.27.4
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`<[^>]+>`)
fmt.Println(re.ReplaceAllString("<p>Hello <b>World</b></p>", ""))
}
Hello World
Java v27.0.1
public class Main {
public static void main(String[] args) {
System.out.println("<p>Hello <b>World</b></p>".replaceAll("<[^>]+>", ""));
}
}
Hello World
JavaScript v26.13.1
// Node has no DOM parser; regex is the common hand-rolled option (full parsing needs DOMParser/htmlparser2)
console.log("<p>Hello <b>World</b></p>".replace(/<[^>]+>/g, ""));
Hello World
PHP v8.6.0
$s = '<p>Hello <b>World</b></p>';
echo strip_tags($s), "\n", strip_tags($s, '<b>');
Hello World Hello <b>World</b>
Python v3.15.2
from html.parser import HTMLParser
# stdlib HTMLParser: skip all tags, collect text nodes only
class Stripper(HTMLParser):
def __init__(self):
super().__init__()
self.parts = []
def handle_data(self, data):
self.parts.append(data)
p = Stripper()
p.feed("<p>Hello <b>World</b></p>")
print("".join(p.parts))
Hello World
Rust v1.99.1
// hand-written finite state machine: drop between '<' and '>'
fn strip_tags(s: &str) -> String {
let mut out = String::new();
let mut in_tag = false;
for c in s.chars() {
if c == '<' {
in_tag = true;
} else if c == '>' {
in_tag = false;
} else if !in_tag {
out.push(c);
}
}
out
}
fn main() {
println!("{}", strip_tags("<p>Hello <b>World</b></p>"));
}
Hello World
TypeScript v26.13.1
// Node has no DOM parser; regex is the common hand-rolled option (full parsing needs DOMParser/htmlparser2)
console.log("<p>Hello <b>World</b></p>".replace(/<[^>]+>/g, ""));
Hello World
Language differences
C: no regex or containers — a finite state machine scans char by char (example); whitelist-keeping requires comparing tag names before dropping.
C++: the same state machine as C (example); it is the zero-dependency approach without std::regex, and a regex version works too when available.
C#: global regex replace <[^>]+> (example); beware that regex mis-truncates malformed HTML whose attribute values contain >.
Go: regexp.MustCompile compiles once for reuse (example); the regex approach shares the same limits on malformed input.
Java: one-line replaceAll (example); production-grade sanitization should use Jsoup (whitelist sanitize).
JavaScript: regex replace is the only lightweight option under Node (the example comment notes full parsing needs DOMParser/htmlparser2); in browsers the DOM textContent extraction works and is injection-proof by construction.
PHP: strip_tags' second parameter whitelists tags to keep (the example keeps <b>, attributes included); note strip_tags is not an XSS defense — event attributes like onerror on kept tags survive, so still escape with htmlspecialchars before output.
Python: the standard-library HTMLParser is a real fault-tolerant parser (example), steadier than regex; whitelisting can be done by overriding handle_starttag to filter tags.
Rust: a hand-written state machine (example); the scraper/lol_html crates offer parser-level whitelist sanitization.
TypeScript: same as JavaScript (example); the type system adds no guarantee here — sanitization is text processing, not a security boundary.