我有一个 PHP 字符串。
$str = "1.testone (off) 2.testtwo (off) 3.testthree (off)";
我需要将字符串拆分为“.”。和“(”。
我知道我可以在“.”处分割字符串。与:
$str1 =explode('.', $str);
这会将字符串放入数组中,数组项位于“.”之间。有什么方法可以用“.”之间的数组项创建一个数组吗?和“(”,并且要么剪掉其余部分,要么将其保留在数组中,但在两个不同的位置分解字符串。
最佳答案
在 explode 中使用 explode ,并结合 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);
产品:
Array ( [0] => 1 [1] => testone [2] => testtwo [3] => testthree )
关于php - 在 2 个不同的位置拆分/分解 PHP 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16868143/