PHP - remove <img> tag from string

PHP - remove <img> tag from string

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

Stefan Bogdanescu

Founder & Senior Architect

Removing <img> Tags from Strings in PHP

Stripping HTML tags from a string is a common requirement when processing user input, cleaning up scraped content, or sanitizing data before storage. However, simply removing all tags often breaks the rendering of text that was embedded within those elements. If your goal is specifically to delete <img> tags while preserving the surrounding text and attributes (or replacing them with a placeholder), you need to be precise about what constitutes an "image" in the context of HTML parsing.

The most straightforward approach for this specific task is using regular expressions via preg_replace. The core logic involves defining a pattern that matches the opening <img> tag, ignores the content between tags (since images don't have inner text), and matches up to the corresponding closing >.

Consider the following regex pattern:

'/<img\s[^>]*>/i'

This pattern breaks down into specific components. The literal string /<img anchors the match to the start of the tag. The \s accounts for optional whitespace between the tag name and its attributes. The [^>]* is a character class that matches any character except a closing angle bracket, repeated zero or more times. This ensures the engine captures all attributes (like src, alt, width) regardless of their order or spacing. Finally, the trailing /> closes the tag match for self-closing syntax, and the i flag makes the operation case-insensitive, handling <IMG> as well as <img>.

When you replace this entire matched string with an empty string or a placeholder like (image), you effectively remove the visual element while keeping the flow of text intact. Here is how you might implement it:

$content = '<p>Check out our <img src="logo.png" alt="Logo"> site.</p>';

// Remove the tags entirely
$cleaned = preg_replace('/<img\s[^>]*>/i', '', $content);

// Or replace with a placeholder text
$placeholder = preg_replace('/<img\s[^>]*>/i', '(image)', $content);

echo $cleaned; 
// Output: <p>Check out our  site.</p>

echo $placeholder;
// Output: <p>Check out our (image) site.</p>

It is crucial to understand the limitations of this regex-based approach. Regular expressions are powerful, but they are not HTML parsers. They operate on character patterns, not semantic structure. This distinction becomes problematic with malformed HTML or complex scenarios where tags are nested incorrectly. While <img> tags are self-closing and theoretically shouldn't have a closing </img>, sloppy coding might introduce them. The regex provided above handles the standard self-closing syntax but would fail to match a stray </img> tag if it ever appeared, leaving that specific malformed tag in your string.

Furthermore, relying solely on regex for heavy HTML sanitization can lead to security issues if you are stripping tags from untrusted input without also validating or escaping the resulting content. If you are building a larger application where this logic is part of a broader data processing pipeline, using a dedicated library like DOMDocument or a robust sanitizer like htmlpurifier provides much better control over attributes and entity encoding.

If you are working within the Laravel ecosystem, you might find utility in existing helper functions or packages that handle HTML manipulation, but for simple string replacement tasks, the native preg_replace function remains efficient and sufficient. Always test your patterns against edge cases, such as images with no attributes (<img src="x">) versus those with extensive data attributes, to ensure your regex behaves predictably in production environments.

Our network