How can PHP developers optimize their code by utilizing preg_replace_callback or PHP 5.3 lambda functions for data extraction tasks?

PHP developers can optimize their code by utilizing preg_replace_callback or PHP 5.3 lambda functions for data extraction tasks by using them to extract specific data from strings efficiently. These functions allow developers to define a callback function to process the matched data dynamically, providing more flexibility and control over the extraction process.

// Example using preg_replace_callback for data extraction
$string = "Hello, my name is John and I am 25 years old.";
$pattern = "/\b(\w+)\b/";
preg_replace_callback($pattern, function($matches) {
    echo "Matched word: " . $matches[0] . "\n";
}, $string);