Prova detta:
$a=array('word1 +word2','word1+word2','word1 -word2',' word1-word2','word1 word2','word1 word2');
foreach ($a as &$v) {
$v=preg_replace('/ +/','|', // last: change blanks to |
preg_replace('/ *(?=[!&])/','', // delete blanks before ! or &
strtr(trim($v),array('-'=>'&!','+'=>'&')) // turn + and - into & and !&
));
}
print_r($a);
Detta ger:
Array
(
[0] => word1&word2
[1] => word1&word2
[2] => word1&!word2
[3] => word1&!word2
[4] => word1|word2
[5] => word1|word2
)