字符串拆分 PHP PREG_MATCH
String Split PHP 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)
);
我希望有人能帮助我..
你应该转义括号
$separator = '#([a-zA-Z0-9. \-\(\)/+]+)-\([a-zA-Z0-9. \-/]+\);{0,1}#';
使用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)
)
(?=-\()
断言匹配后必须跟一个破折号和一个左括号的肯定前瞻。
我有一个字符串:
$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)
);
我希望有人能帮助我..
你应该转义括号
$separator = '#([a-zA-Z0-9. \-\(\)/+]+)-\([a-zA-Z0-9. \-/]+\);{0,1}#';
使用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)
)
(?=-\()
断言匹配后必须跟一个破折号和一个左括号的肯定前瞻。