我想将abc49.99ab55.5def89de7
拆分成块以获得下面的结果(或类似)。 注意:仅允许abc
,ab
,def
和de
。数字可以浮动。
理想情况下:
Array
(
[abc] => 49.99
[ab] => 55.5
[def] => 89
[de] => 7
)
这也很好:
Array
(
[0] => abc49.99
[1] => ab55.5
[2] => def89
[3] => de7
)
在看了一些例子之后,我已经提出了下面的例子,但遗憾的是无法增强它们以满足上面定义的需求。你能帮忙解决这个问题吗?
1
preg_match('/(?<fo>abc|ab)?(?<fn>\d*\.?\d*)(?<so>def|de)?(?<sn>\d*\.?\d*)?/', 'abc49.99ab55.5def89de7', $matches);
Array
(
[0] => abc49.99
[fo] => abc
[1] => abc
[fn] => 49.99
[2] => 49.99
[so] =>
[3] =>
[sn] =>
[4] =>
)
2
preg_match_all('~^(.*?)(\d+)~m', 'abc49.99ab55.5def89de7', $matches);
Array
(
[0] => Array
(
[0] => abc49
)
[1] => Array
(
[0] => abc
)
[2] => Array
(
[0] => 49
)
)
答案 0 :(得分:1)
来自OP的request。这应该适合你:
<?php
$str = "abc49.99ab55.5def89de7";
$arr = preg_split("/(?<=[0-9])(?=[a-z])/i", $str);
print_r($arr);
?>
正则表达式解释:
/(?<=[0-9])(?=[a-z])/i