我有像这样的字符串
selectedvalue == product || product condition || product flush then @"yes,no",selectedvalue == water || water condition || water flush then @"true,false"
。
在preg split中使用一些模式以获得数组中的结果输出。
Array(
0 => selectedvalue == product || product condition || product flush then @"yes,no"
1 => selectedvalue == water || water condition || water flush then @"true,false"
);
任何人都可以帮忙吗?
答案 0 :(得分:1)
<?php
$data='selectedvalue == product || product condition || product flush then @"yes,no",selectedvalue == water || water condition || water flush then @"true,false"';
$data=explode('selectedvalue',$data);
$myArray=[];
foreach($data as $row){
$myArray[]='selectedvalue '.$row;
}
echo '<pre>';
unset($myArray[0]);
print_r($myArray);
您可以尝试使用explode来创建数组。这就是为此功能创建的。
上述代码的输出是:
Array
(
[1] => selectedvalue == product || product condition || product flush then @"yes,no",
[2] => selectedvalue == water || water condition || water flush then @"true,false"
)
这是一个“肮脏”的解决方案,但我认为它将解决您的问题。如果您可以使用管道拆分记录会更好,您可以简单地爆炸到管道分隔符并准备好阵列。
答案 1 :(得分:0)
如果您必须使用preg_split来获取结果,则可以使用正向前瞻(?=selectedvalue)
来匹配逗号,以断言后面的内容为selectedvalue
:
$pattern = "/,(?=selectedvalue)/";
$string = 'selectedvalue == product || product condition || product flush then @"yes,no",selectedvalue == water || water condition || water flush then @"true,false';
var_dump(preg_split($pattern, $string));
那会给你:
array(2) {
[0]=>
string(77) "selectedvalue == product || product condition || product flush then @"yes,no""
[1]=>
string(74) "selectedvalue == water || water condition || water flush then @"true,false"
}