我的输入是$text = '( ( LED AND DIODE ) OR ( "LEE power" and system ) ) '
我正在对此输入应用分解函数
function multiexplode ($delimiters,$string)
{
$ready = str_replace($delimiters, $delimiters[0], $string);
$launch = explode($delimiters[0], $ready);
return $launch;
}
$exploded = multiexplode(array(' ',":" ), $text);
for($i=0;$i<(count($exploded));$i++)
echo "<br> $exploded[$i]";我的输出如下所示
0 - (
1 - (
2 - LED
3 - AND
4 - DIODE
5 - )
6 - OR
7 - (
8 - "LEE
9 - power"
10 - and
11 - system
12 - )
13 - ) 但我希望输出如下所示
0 - (
1 - (
2 - LED
3 - AND
4 - DIODE
5 - )
6 - OR
7 - (
8 - LEE power
9 - and
10 - system
11 - )
12 - ) 我希望将“”中的所有单词存储在数组的一个元素中。
发布于 2017-07-06 15:31:17
检查以下代码:
$text = '( ( LED AND DIODE ) OR ( "LEE power" and system ) ) ';
preg_match_all('/"(?:\.|[^"])*"|\S+/', $text, $exploded);
for($i=0;$i<(count($exploded[0]));$i++)
echo "<br>". $exploded[0][$i];基于comment的更新
对于$text = '((LED AND DIODE) OR ("LEE power" and system)) ';的输入
$text = '((LED AND DIODE) OR ("LEE power" and system)) ';
$re = '/"(?:\.|[^"])*"|\(?|\)|(\'(.*?)\'|(\w+))/';
preg_match_all($re, $text, $exploded);
for($i=0;$i<(count($exploded[0]));$i++)
if($exploded[0][$i]!='')
echo "<br>". $exploded[0][$i];发布于 2017-07-06 16:07:21
虽然这不是一个经过全面测试的答案,但它回答了您所描述的当前问题。祝你好运!
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;
}对于与给定数组相关的特定输出,您可以使用:
//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>";输出:
Array
(
[0] => (
[1] => (
[2] => LED
[3] => AND
[4] => DIODE
[5] => )
[6] => OR
[7] => (
[8] => "LEE power"
[9] => and
[10] => system
[11] => )
[12] => )
)发布于 2017-07-06 15:05:03
这里
<?php
$text = str_replace('LEE power','LEE-power',"( ( LED AND DIODE ) OR ( 'LEE power' and system ) ) ");
$newArray = explode(' ',$text);
foreach($newArray as $row) $data[] = str_replace('-',' ',$row);
echo "<pre>";print_r($data);die;
?>https://stackoverflow.com/questions/44941719
复制相似问题