I can't think of a way to do this with a more complex regex. Depending on how many words in each array value are being matched, it should be fairly easy to explode() the value and reverse them and use them in the match. eg. maybe something like:
PHP Code:
$val_combo = array();
foreach($product_array as $one_product)
{
$val_arr = explode(' ', $one_product);
if(count($val_arr) > 1)
{
$val_combo[0] = $val_arr[0] . ' ' . $val_arr[1];
$val_combo[1] = $val_arr[1] . ' ' . $val_arr[0];
}
else
{
$val_combo[0] = $one_product;
}
$combo_len = count($val_combo);
for($i = 0; $i < $combo_len; $i++)
{
if (preg_match("/$val_combo[$i]/i", $string, $matches, PREG_OFFSET_CAPTURE))
{
return $matches[0];
}
}
}
If there are always only 2 words, you could simplify this a bit. If there are sometimes more than 2 words you could either tweak this a bit or add some recursive re-ordering code to it. But you get the idea. Or you could just match the individual words without rearranging them if that would work.
IMHO this would be slower than adding them to the original array, but it definately would be easier than manually adding them to the array. You probably should consider putting something together that would add the rearranged values to the array one-time for you.
Bookmarks