While this isn't a thoroughly-tested answer, this answers your current issue as you described it. Best of luck!
function concatenateQuotedElements($input_array)
{
//As per the question, we'll be exploding our array by spaces " ".
$pieces = explode(" ", $input_array);
//This array will be returned as our final array of concatenated elements.
$output_array = array();
/*When we happen upon an element containing a
parenthesis ('"') as the first character, we'll
temporarily store so that we may concatenate
future elements with it.*/
$stored_element = null;
$stored_position = null;
/*Parse through our array and look for any character
that contains a " and see whether it's at the beginning
or the end of the string element.*/
for ($i = 0; $i < count($pieces); $i++)
{
//If we detect a parenthesis in our element...
if (strpos($pieces[$i], '"') !== false)
{
//See if it's at the beginning, and if it is, store it or future use.
if (substr($pieces[$i], 0, 1) === '"')
{
$stored_element = $pieces[$i];
$stored_position = $i;
}
/*Or, see if it's at the end of the string. If it is, we need to see
if there's been a previous element with an opening parenthesis that
we've stored and concatenate this element onto our stored element.*/
if (
substr($pieces[$i], -1) === '"' &&
$stored_element !== null &&
$stored_position !== null
)
{
$output_array[$stored_position] = $stored_element . " " . $pieces[$i];
/*Reset our temporary storage so that it hold
any forthcoming elements with parenthesis.*/
$stored_element = null;
$stored_position = null;
/*Finally, [Continue] to the next element without
pushing this as a non-concatenated, standalone element.*/
continue;
}
}
array_push($output_array, $pieces[$i]);
}
return $output_array;
}
For a specific output pertaining to your given array, you can use:
//Our example array given in the question:
$array = '( ( LED AND DIODE ) OR ( "LEE power" and system ) )';
//Our output:
$concatenate_quotes_array = concatenateQuotedElements($array);
//"Pretty print" the array result of the function
echo "<pre>";
print_r($concatenate_quotes_array);
echo "</pre>";
Output:
Array
(
[0] => (
[1] => (
[2] => LED
[3] => AND
[4] => DIODE
[5] => )
[6] => OR
[7] => (
[8] => "LEE power"
[9] => and
[10] => system
[11] => )
[12] => )
)