php - 在 2 个不同的位置拆分/分解 PHP 字符串

标签 php arrays string explode

我有一个 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/

相关文章:

php - Codeigniter:加载 session 库时出现错误

java - 如何将分割数组的其余部分转换为java上的字符串?

php - 为包括 Google 在内的多个客户编写 ICS 文件

php - 无法在 Ubuntu 14.04 上安装 Mysql 5.6

arrays - 从 bash 输出 bash 引用数组

c# - Convert.ToString 返回 string.empty 而不是 null

java - 正则表达式:如何处理空引号

string - 字符串中的 MATLAB 行继续

php - 从具有多个 ID 的同一个 Mysql 列中获取数据

php - 从邻接列表数据获取路径