What are the advantages and disadvantages of using preg_match versus preg_replace for filtering out PHP commands in PHP code?

When filtering out PHP commands in PHP code, using preg_match allows you to check if the code contains any unwanted commands without modifying the original code. On the other hand, preg_replace not only checks for the commands but also replaces them with a specified value, which may alter the original code unintentionally.

// Using preg_match to filter out PHP commands
$code = "echo 'Hello World';";
if (preg_match('/\b(?:eval|exec|system)\b/i', $code)) {
    echo "Unwanted commands found!";
} else {
    echo "Code is safe.";
}