What are the best practices for filtering content from a text file using preg_match_all in PHP?

When filtering content from a text file using preg_match_all in PHP, it is important to use proper regular expressions to accurately capture the desired content. It is also recommended to sanitize the input data to prevent any security vulnerabilities. Additionally, consider using the flags parameter in preg_match_all to customize the behavior of the function.

// Read the contents of the text file
$file_contents = file_get_contents('example.txt');

// Define the regular expression pattern
$pattern = '/[A-Za-z0-9]+/';

// Perform the matching
preg_match_all($pattern, $file_contents, $matches);

// Output the filtered content
print_r($matches[0]);