What best practices should be followed when attempting to remove comments from PHP code using regular expressions?

When attempting to remove comments from PHP code using regular expressions, it is important to consider both single-line comments starting with "//" and multi-line comments enclosed within "/* */". To handle both cases, a regular expression pattern can be used to match and remove these comment sections from the code.

$code = file_get_contents('example.php');

// Remove single-line comments
$code = preg_replace('/\/\/.*$/m', '', $code);

// Remove multi-line comments
$code = preg_replace('/\/\*(.*?)\*\//s', '', $code);

echo $code;