What are the advantages and disadvantages of using preg_grep compared to foreach() and strpos() when working with text files in PHP?
When working with text files in PHP, using preg_grep can be advantageous because it allows for more flexible pattern matching using regular expressions. This can make it easier to search for specific patterns or values within a text file. However, using preg_grep may be slower than using a combination of foreach() and strpos() for simple string searches, as regular expressions can be more resource-intensive.
// Using preg_grep to search for a specific pattern in a text file
$lines = file('example.txt');
$pattern = '/\bexample\b/';
$matches = preg_grep($pattern, $lines);
foreach($matches as $match){
echo $match . "\n";
}