【问题标题】:Explode that doesn't look inside parenthesis PHP爆炸不会在括号内 PHP
【发布时间】:2015-01-20 20:14:58
【问题描述】:

我有类似下面的字符串

MATH 2822 or (PHYS 1939 and PHYS 1238)
MATH 2881 and (CHEM 2184 or PHYS 1000)
MATH 2881 and (CHEM 2184 or PHYS 1000) or ELEC 2921
MATH 2881 and ELEC 2921

基本上我想在他们身上做一个explode() 来分割需求,但是爆炸不应该影响括号内的任何东西。

例如:

$str = MATH 2881 and (CHEM 2184 or PHYS 1000) or ELEC 2921
/* explode on "or" result as follows */
$arr[0] = Math 2881 and (CHEM 2184 or PHYS 1000)
$arr[1] = ELEC 2921

如您所见,它不应该拆分括号内的两个元素。

什么是这样做的好方法?

【问题讨论】:

标签: php arrays string explode


【解决方案1】:

您可以使用多个爆炸并查找括号。

以“(”作为参数展开。

这应该给你一个看起来像的数组

$arr[0] = Math 2881 and;
$arr[1] = CHEM 2184 or PHYS 1000) ELEC 2921;

然后用 ")" 作为参数分解 $arr[1],您可以将其作为新变量获取

$Narr[0]=CHEM 2184 or PHYS 1000;
$Narr[1]=or ELEC 2921;

然后你可以把它们放在一个完成的数组中 $FINarr=array(0=>$arr[0],1=>$Narr[0],2=>$Narr[1]);

$FINarr[0]=Math 2881 and;
$FINarr[1]=HEM 2184 or PHYS 1000;
$FINarr[2]=or ELEC 2921;

【讨论】:

    猜你喜欢
    • 2020-02-21
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多