How To Remove Images with DOMDocument

I’ve written about DOMDocument in a few other posts (1, 2, 3 to share a few) but I continue to find it useful in different ways.

Remember, DOMDocument is a class in PHP that allows us to manipulate the HTML document before rendering it in the browser.

From the manual:

Represents an entire HTML or XML document; serves as the root of the document tree.

Whenever I think of working with the saveHTML function, I think of needing to serialize the new information into a file or other output stream before sending it to the browser.

But we don’t have to do that. It can be done in memory.

Remove Images with DOMDocument

Case in point: In a project, I need to find all of the image elements in a document. If the image doesn’t have a valid source URL, then I need to either replace the source attribute or remove it.

Remove Images with DOMDocument

For the purposes of this example, assume the following

  • isValidImage is a function that’s responsible for checking to see if the specified source attribute resolves to a valid URL,
  • That I’m setting the src attribute to an empty string rather than a placeholder image to keep the code simple,
  • And I return the result to whatever will be rendered in the template or the partial.

I’ve commented the code so it should be easy enough to follow for this example:

Notice that there are several ways the code above could be improved (such as returning early if no images are found), but the purpose is to show how to replace the src attribute if the URL doesn’t properly resolve. Thus, removing images with DOMDocument.

If you’re interested on how to determine if a URL is valid, this article may be helpful.