How can the issue of getting the content of a file multiple times be resolved when using preg_replace_callback in PHP?

When using preg_replace_callback in PHP, the issue of getting the content of a file multiple times can be resolved by reading the content of the file outside of the callback function and passing it as an argument to the callback function. This way, the file content is only read once and passed to the callback function each time it is called.

$file_content = file_get_contents('example.txt');

function callback_function($matches) {
    global $file_content;
    // Process the matches using $file_content
}

$output = preg_replace_callback('/pattern/', 'callback_function', $file_content);