How can the preg_match function be optimized for better performance in PHP?
The preg_match function in PHP can be optimized for better performance by using the 'preg_match_all' function instead when you need to match multiple occurrences. This reduces the overhead of calling preg_match multiple times and can improve performance significantly.
// Original code using preg_match
preg_match('/pattern/', $string, $matches);
// Optimized code using preg_match_all
preg_match_all('/pattern/', $string, $matches);