如何使用 php preg_split 从字符串中获取除括号外的所有内容?
How to get everything except brackets from string using php preg_split?
$str = "[10:42-23:10]part1[11:30-13:20]part2"
我希望将其拆分为:
[1] 10:42-23:10
[2] part1
[3] 11:30-13:20
[4] part2
我想出的最好办法是:
$parts = preg_split("/(\[*\])\w+/", $str );
但是这个returns
[0] => [10:42-23:10
[1] => [11:30-13:20
[2] =>
在 [
和 ]
之间拆分选择,并使用标志 PREG_SPLIT_NO_EMPTY
不捕获空部分。
$str = "[10:42-23:10]part1[11:30-13:20]part2";
$parts = preg_split("/\[|\]/", $str, -1, PREG_SPLIT_NO_EMPTY );
print_r($parts);
输出:
Array
(
[0] => 10:42-23:10
[1] => part1
[2] => 11:30-13:20
[3] => part2
)
注意.
感谢@WiktorStribiżew,他的正则表达式 /[][]/
效率更高,我有一些基准测试,它快了大约 40%。
$str = "[10:42-23:10]part1[11:30-13:20]part2";
$parts = preg_split("/[][]/", $str, -1, PREG_SPLIT_NO_EMPTY );
print_r($parts);
这是我用来做基准测试的 perl 脚本:
#!/usr/bin/perl
use Benchmark qw(:all);
my $str = "[10:42-23:10]part1[11:30-13:20]part2";
my $count = -5;
cmpthese($count, {
'[][]' => sub {
my @parts = split(/[][]/, $str);
},
'\[|\]' => sub {
my @parts = split(/\[|\]/, $str);
},
});
结果:(2 次运行)
>perl -w benchmark.pl
Rate \[|\] [][]
\[|\] 536640/s -- -40%
[][] 891396/s 66% --
>Exit code: 0
>perl -w benchmark.pl
Rate \[|\] [][]
\[|\] 530867/s -- -40%
[][] 885242/s 67% --
>Exit code: 0
使用简单的正则表达式匹配任何 [...]
子字符串(\[[^][]*]
) and wrap the whole pattern with a capturing group - then you can use it with preg_split
和 PREG_SPLIT_DELIM_CAPTURE
标志以获取匹配之间的捕获和子字符串:
$re = '/(\[[^][]*])/';
$str = '[10:42-23:10]part1[11:30-13:20]part2';
$matches = preg_split($re, $str, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
print_r($matches);
使用这种方法,您可以更好地控制方括号内的匹配内容,因为您可以调整模式以仅匹配时间范围,例如
(\[\d{2}:\d{2}-\d{2}:\d{2}])
A [10:42-23:10]part1[11:30-13:20]part2[4][5]
将拆分为 [10:42-23:10]
、part1
、[11:30-13:20]
和 part2[4][5]
(注意 [4][5]
未拆分).
您也可以在 preg_match_all()
instead of preg_split()
中使用正则表达式
$str = "[10:42-23:10]part1[11:30-13:20]part2";
preg_match_all("/[^\[\]]+/", $str, $parts);
print_r($parts[0]);
结果见demo
没有正则表达式,您可以使用 strtok
:
$result = [];
$tok = strtok($str, '[]');
do {
if (!empty($tok))
$result[] = $tok;
} while (false !== $tok = strtok('[]'));
$str = "[10:42-23:10]part1[11:30-13:20]part2"
我希望将其拆分为:
[1] 10:42-23:10
[2] part1
[3] 11:30-13:20
[4] part2
我想出的最好办法是:
$parts = preg_split("/(\[*\])\w+/", $str );
但是这个returns
[0] => [10:42-23:10
[1] => [11:30-13:20
[2] =>
在 [
和 ]
之间拆分选择,并使用标志 PREG_SPLIT_NO_EMPTY
不捕获空部分。
$str = "[10:42-23:10]part1[11:30-13:20]part2";
$parts = preg_split("/\[|\]/", $str, -1, PREG_SPLIT_NO_EMPTY );
print_r($parts);
输出:
Array
(
[0] => 10:42-23:10
[1] => part1
[2] => 11:30-13:20
[3] => part2
)
注意.
感谢@WiktorStribiżew,他的正则表达式 /[][]/
效率更高,我有一些基准测试,它快了大约 40%。
$str = "[10:42-23:10]part1[11:30-13:20]part2";
$parts = preg_split("/[][]/", $str, -1, PREG_SPLIT_NO_EMPTY );
print_r($parts);
这是我用来做基准测试的 perl 脚本:
#!/usr/bin/perl
use Benchmark qw(:all);
my $str = "[10:42-23:10]part1[11:30-13:20]part2";
my $count = -5;
cmpthese($count, {
'[][]' => sub {
my @parts = split(/[][]/, $str);
},
'\[|\]' => sub {
my @parts = split(/\[|\]/, $str);
},
});
结果:(2 次运行)
>perl -w benchmark.pl
Rate \[|\] [][]
\[|\] 536640/s -- -40%
[][] 891396/s 66% --
>Exit code: 0
>perl -w benchmark.pl
Rate \[|\] [][]
\[|\] 530867/s -- -40%
[][] 885242/s 67% --
>Exit code: 0
使用简单的正则表达式匹配任何 [...]
子字符串(\[[^][]*]
) and wrap the whole pattern with a capturing group - then you can use it with preg_split
和 PREG_SPLIT_DELIM_CAPTURE
标志以获取匹配之间的捕获和子字符串:
$re = '/(\[[^][]*])/';
$str = '[10:42-23:10]part1[11:30-13:20]part2';
$matches = preg_split($re, $str, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
print_r($matches);
使用这种方法,您可以更好地控制方括号内的匹配内容,因为您可以调整模式以仅匹配时间范围,例如
(\[\d{2}:\d{2}-\d{2}:\d{2}])
A [10:42-23:10]part1[11:30-13:20]part2[4][5]
将拆分为 [10:42-23:10]
、part1
、[11:30-13:20]
和 part2[4][5]
(注意 [4][5]
未拆分).
您也可以在 preg_match_all()
instead of preg_split()
$str = "[10:42-23:10]part1[11:30-13:20]part2";
preg_match_all("/[^\[\]]+/", $str, $parts);
print_r($parts[0]);
结果见demo
没有正则表达式,您可以使用 strtok
:
$result = [];
$tok = strtok($str, '[]');
do {
if (!empty($tok))
$result[] = $tok;
} while (false !== $tok = strtok('[]'));