What are best practices for debugging PHP code when encountering issues with preg_replace or other regex functions?

When encountering issues with preg_replace or other regex functions in PHP, it is important to first check the regular expression pattern for any syntax errors or unexpected characters. Additionally, ensure that the input string being passed to the function matches the expected format. Using the preg_last_error() function can also help identify any errors in the regex pattern.

$pattern = '/[0-9]+/';
$input = 'abc123xyz';
$replacement = '';

if (preg_match($pattern, $input)) {
    $output = preg_replace($pattern, $replacement, $input);
    echo $output;
} else {
    echo 'No match found';
}