How can PHP developers optimize the usage of preg_match to efficiently search for multiple strings within an array?

When searching for multiple strings within an array using preg_match, PHP developers can optimize the process by using the "|" operator to create a single regular expression pattern that matches all the strings. This way, they can avoid multiple preg_match calls and improve the efficiency of the search.

<?php

// Array of strings to search within
$array = ['apple', 'banana', 'cherry', 'date'];

// Strings to search for
$searchStrings = ['apple', 'cherry'];

// Create a single regular expression pattern using the "|" operator
$pattern = '/'.implode('|', array_map('preg_quote', $searchStrings)).'/';

// Iterate through the array and search for the strings efficiently
foreach ($array as $item) {
    if (preg_match($pattern, $item)) {
        echo "$item matches one of the search strings.\n";
    }
}

?>