strip_tags() .... replace tags by space rather than deleting them

strip_tags() .... replace tags by space rather than deleting them

  1. Blog
  2. 15 November 2026
  3. 2 min read

Stefan Bogdanescu

Founder & Senior Architect

strip_tags() .... replace tags by space rather than deleting them

When you encounter HTML strings in PHP applications, the go-to utility for sanitization is often strip_tags(). The function performs a straightforward operation: it removes all recognized HTML tags from a string. However, the default behavior can lead to unexpected whitespace issues that break layout or parsing logic downstream. A common scenario involves processing content like <h1>Foo</h1>bar. If you pass this directly to strip_tags(), the result is foobar. The closing bracket of the tag and the opening letter of the next word sit immediately adjacent, creating a single concatenated string that loses semantic separation.

To resolve this, you need to replace tags with a space character instead of deleting them entirely. This ensures that words remain distinct even when the structural markers are removed. You can achieve this by wrapping your input in a temporary container that strip_tags() will respect regarding whitespace, or by leveraging a more granular approach using regular expressions if you require specific control over the output format.

Consider the following implementation pattern:

$input = '<h1>Foo</h1>bar';

// Method 1: Simple replacement before stripping
// Note: strip_tags() removes tags but does not preserve whitespace automatically
$safeInput = preg_replace('/<\/?[^>]+>/s', ' ', $input);
$output = trim($safeInput);

echo $output; // Outputs: "Foo bar"

By replacing the tags with a space using preg_replace before or during the cleaning process, you ensure that the text flow remains intact. The regex pattern /\/?[^>]+>/s targets any sequence of characters that starts with <, ends with >, and handles both opening and closing tags while ignoring nested attributes or self-closing syntax variations. This approach is robust because it treats the HTML markup as a distinct entity separate from the text content.

There is a subtle trade-off to consider when using strip_tags(). The function does not parse the document structure; it simply strips known tag names. If you strip tags without preserving whitespace, you risk merging adjacent text nodes. For instance, <b>Bold</b> and <i>Italic</i> becomes BoldandItalic if handled naively. This can cause issues in search indexing, accessibility tools, or downstream rendering engines that expect standard word boundaries.

Furthermore, relying solely on strip_tags() for security is a misconception. If your goal is to sanitize user input to prevent XSS attacks, simply removing tags might not be sufficient depending on the context. An attacker could inject JavaScript within attributes that survive a naive strip operation if you are not careful with attribute handling. However, in cases where you specifically need to extract plain text while maintaining word separation, replacing tags with spaces is the correct architectural decision.

If you are working within the Laravel framework, you might look toward packages or helper functions that abstract this logic, but understanding the underlying string manipulation remains crucial for debugging complex text processing pipelines. The core principle remains: do not assume that removing a tag leaves behind an invisible gap; explicitly define the replacement value to control the resulting whitespace behavior.

Our network