Compress svg files in PHP
SVG is structured data and should therefore not require regex to work with it.  I would suggest that you work with either an XML or SVG-specific library in PHP which would let you parse in the input SVG, remove any unwanted nodes and then render it back out.

Here is a starting point for common PHP tools for working with XML that should be able to do what you want.

This also looks like a library that could meet your needs.