字符串拆分PHP PREG_MATCH

时间:2015-01-21 11:53:38

标签: php arrays split preg-match

我有一个字符串:

$productList="
Saluran Dua(Bothway)-(TAN007);
Memo-(T-Memo);
7-Pak FBIP  R-100.000-(F1R100);
FBIP LOKAL 350 Mnt  (R/N/M)-(FL350);
FBIP SLJJ 30+300 Mnt (R)-(FJ30R)
";

我想要这样的结果:

Array(
[0]=>Saluran Dua(Bothway)
[1]=>Memo
[2]=>7-Pak FBIP  R-100.000
[3]=>FBIP LOKAL 350 Mnt  (R/N/M)
[4]=>FBIP SLJJ 30+300 Mnt (R)
);

我用过:

$separator = '#\;([a-zA-Z/0-9()\- ]*)\-#';
preg_match_all($separator, $productList, $match);
$value=$match[1];

但结果是:

Array(
[0] => Saluran Dua Arah(BothWay)
[1] => Memo-(T
[2] => 7-Pak FBIP  R
[3] => FBIP LOKAL 350 Mnt  (R/N/M)
);

我希望有人可以帮助我..

2 个答案:

答案 0 :(得分:0)

你应该逃避括号

$separator = '#([a-zA-Z0-9. \-\(\)/+]+)-\([a-zA-Z0-9. \-/]+\);{0,1}#';

答案 1 :(得分:0)

使用preg_match_all功能。

$productList= <<< EOT
Saluran Dua(Bothway)-(TAN007);
Memo-(T-Memo);
7-Pak FBIP  R-100.000-(F1R100);
FBIP LOKAL 350 Mnt  (R/N/M)-(FL350);
FBIP SLJJ 30+300 Mnt (R)-(FJ30R)
EOT;
preg_match_all('~(?m)^.*?(?=-\()~', $productList, $match);
print_r($match[0]);

输出:

Array
(
    [0] => Saluran Dua(Bothway)
    [1] => Memo
    [2] => 7-Pak FBIP  R-100.000
    [3] => FBIP LOKAL 350 Mnt  (R/N/M)
    [4] => FBIP SLJJ 30+300 Mnt (R)
)

(?=-\()肯定前瞻,断言匹配必须后跟短划线和左括号。