How can PHP developers optimize their code when using Preg_Match for text processing tasks?

When using Preg_Match for text processing tasks in PHP, developers can optimize their code by using the 'preg_match_all' function instead of 'preg_match' when they need to match multiple occurrences of a pattern in a string. This can help reduce the number of function calls and improve performance.

// Original code using preg_match
$pattern = '/[0-9]+/';
$string = 'There are 123 apples and 456 oranges';
preg_match($pattern, $string, $matches);

// Optimized code using preg_match_all
$pattern = '/[0-9]+/';
$string = 'There are 123 apples and 456 oranges';
preg_match_all($pattern, $string, $matches);