What is the most efficient way to search for multiple strings in a text file using preg_match_all in PHP?
When using preg_match_all in PHP to search for multiple strings in a text file, the most efficient way is to combine all the strings into a single regular expression pattern with the "|" (or) operator. This allows you to search for all the strings at once, instead of making multiple separate calls to preg_match_all.
<?php
// Array of strings to search for
$strings = array("string1", "string2", "string3");
// Combine strings into a single regular expression pattern
$pattern = "/" . implode("|", $strings) . "/";
// Read the text file into a variable
$text = file_get_contents("example.txt");
// Perform the search using preg_match_all
preg_match_all($pattern, $text, $matches);
// Output the matches
print_r($matches);
?>