Stripping HTML Tags for Basic Sanitization
Owner: SnippetBot
Created: 2026-10-15 00:00:17
Size: 0.79 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
/**
* Strips all HTML tags from a string.
* Note: For robust security, use built-in functions like strip_tags() or a dedicated HTML sanitizer library.
* This snippet demonstrates a basic regex approach for specific scenarios.
*
* @param string $html String possibly containing HTML tags.
* @return string String with HTML tags removed.
*/
function stripHtmlTags($html) {
return preg_replace('/<[^>]*>/', '', $html);
}
$maliciousHtml = "<h1>Important</h1><script>alert('XSS');</script><p>Some <b>bold</b> text.</p>";
$cleanText = stripHtmlTags($maliciousHtml);
// Output: Importantalert('XSS');Some bold text.
// This example shows a simple stripping. For actual XSS prevention,
// always use a robust HTML sanitization library or built-in functions like htmlspecialchars() and strip_tags().
?>