Remove all html tags from php string

Remove all html tags from php string

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

Stefan Bogdanescu

Founder & Senior Architect

Removing HTML Tags from PHP Strings Before Truncation

Truncating text to display a preview—like showing the first 110 characters of a business_description field—is a common requirement for web applications. The naive approach often looks something like this:

<?php echo substr($row_get_Business['business_description'], 0, 110) . "..."; ?>

While this works perfectly for plain text, it breaks down when the database contains HTML content entered by users. If a client inputs <p class="Body1"><strong>Ref no:</strong></p>, your code will render that raw markup or display broken characters instead of clean text. The immediate goal is to strip out all HTML tags before applying the substr() function, ensuring you only count visible characters and not the length of closing brackets.

Why Simple Regex Fails

The most instinctive solution for many developers is to reach for a regular expression. You might try using preg_replace with a pattern like /<.*?>/ to strip tags. However, this approach is notoriously fragile in real-world scenarios. HTML tags can nest (e.g., <div><span>text</span></div>), and attributes can contain special characters or even quotes that confuse a simple regex engine.

Furthermore, relying on regex to parse HTML violates the principle of using the right tool for the job. It is not designed for structural parsing. If your goal is just to strip tags for display purposes in PHP, the strip_tags() function exists specifically for this and handles the vast majority of standard cases more reliably than a custom regular expression.

Using strip_tags() Correctly

The built-in strip_tags() function removes all HTML and PHP tags from a string. You can pipe your truncated data through it before displaying the result:

<?php 
$raw_text = $row_get_Business['business_description'];
$cleaned_text = strip_tags($raw_text);
echo substr($cleaned_text, 0, 110) . "..."; 
?>

This ensures that no matter how complex the user's input is—whether they used <b>, <strong>, or nested divs—the output will be plain text. This method is efficient, requires no external dependencies, and is the standard practice for simple tag removal in PHP.

The Truncation Trap: Whitespace and Empty Strings

While strip_tags() handles the markup, you must be careful about how truncation interacts with whitespace. When a user enters <p> </p>, strip_tags() converts that to spaces. If your database entry is mostly whitespace or if the tag stripping leaves behind excessive newlines, your preview might look like:

...   ...

To prevent this, you should trim and normalize whitespace before truncating. This prevents a block of spaces from being counted as part of the character limit. Here is a more robust implementation:

<?php 
$raw_text = $row_get_Business['business_description'];
$cleaned_text = strip_tags($raw_text);
$trimmed_text = trim(preg_replace('/\s+/', ' ', $cleaned_text)); // Normalize whitespace
echo substr($trimmed_text, 0, 110) . "..."; 
?>

This ensures that multiple spaces or newlines are collapsed into a single space, making the preview much cleaner.

Security Considerations

Stripping tags is often done to sanitize output for display, but it is not a security measure against XSS (Cross-Site Scripting). If your database stores user-generated content containing <script>alert('XSS')</script>, simply removing the < and > brackets with regex or strip_tags() might not be enough if you aren't also escaping other characters.

In this specific case, since you are stripping all tags, any scripts inside them will be removed along with the markup. However, if your strategy changes to only removing specific tags (like <script> but keeping others), you must ensure that attribute values are escaped using htmlspecialchars(). Always treat database content as untrusted until it is rendered in a context where escaping or sanitization has occurred.

Conclusion

For truncating HTML-laden strings in PHP, relying on strip_tags() combined with whitespace normalization provides a safe and effective solution. It avoids the complexity of regex parsing while ensuring that the character count reflects actual visible text rather than markup artifacts.

Our network