How can regular expressions be optimized for better performance in PHP code?

Regular expressions can be optimized for better performance in PHP code by using specific modifiers and functions. One way to improve performance is by using the "i" modifier to make the regular expression case-insensitive. Additionally, using functions like preg_match() instead of preg_match_all() can also help optimize performance.

// Example of optimizing regular expressions for better performance in PHP code

// Original regular expression
$pattern = '/[0-9]+/';

// Optimized regular expression with "i" modifier for case-insensitivity
$pattern = '/[0-9]+/i';

// Using preg_match() instead of preg_match_all() for better performance
$string = "12345";
if (preg_match($pattern, $string, $matches)) {
    echo "Match found!";
}