拆分/爆炸一个PHP字符串在2个不同的地方


Split/Explode a PHP String at 2 Different Places

我有一个PHP字符串。

$str = "1.testone (off) 2.testtwo (off) 3.testthree (off)";

我需要在"。"answers"("之间分割字符串。

我知道我可以在"."处分割字符串:

$str1 = explode('.', $str);

这将字符串放入数组中,数组项位于"。"之间。是否有任何方法可以在"。"answers"("之间的数组项创建一个数组,并且要么剪掉其余部分,要么将其保留在数组中,但在2个不同的位置爆炸字符串。

在explosion中使用explosion,并结合foreach循环。

$str = "1.testone (off) 2.testtwo (off) 3.testthree (off)";
$explode1 = explode('.', $str);
$array = array();
foreach($explode1 as $key => $value) {
$explode2 = explode('(', $explode1[$key]);
array_push($array, $explode2[0]);
}
print_r($array);

生产:

阵列([0]=> 1 [1]=> testone [2] => testtwo [3] => testthree)

<?php
    $str = "1.testone (off) 2.testtwo (off) 3.testthree (off)";
    $result = preg_split("/'.|'(/", $str);
    print_r($result);
?>
结果:

Array
(
    [0] => 1
    [1] => testone 
    [2] => off) 2
    [3] => testtwo 
    [4] => off) 3
    [5] => testthree 
    [6] => off)
)
$str = "1.testone (off) 2.testtwo (off) 3.testthree (off)";
$arr = array();
foreach(explode('.',$str) as $row){
    ($s=strstr($row,'(',true)) && $arr[] = $s;
}
print_r($arr);
//Array ( [0] => testone [1] => testtwo [2] => testthree )