What are some potential pitfalls when using preg_match and preg_replace in PHP?

One potential pitfall when using preg_match and preg_replace in PHP is not properly handling error conditions, such as when the pattern is invalid or the replacement string is not provided. To solve this, always check the return value of preg_match and preg_replace to ensure they executed successfully before using the results.

// Check if preg_match was successful before using the result
if (preg_match('/pattern/', $string, $matches)) {
    // Use $matches array here
} else {
    // Handle error condition
}

// Check if preg_replace was successful before using the result
$result = preg_replace('/pattern/', 'replacement', $string);
if ($result !== null) {
    // Use $result here
} else {
    // Handle error condition
}